0% found this document useful (0 votes)
6 views9 pages

PWP Notes 6

This document covers file input/output (I/O) handling and exception handling in Python. It explains how to perform various file operations such as opening, reading, writing, and managing files and directories, as well as the use of try-except blocks for handling exceptions. Additionally, it includes examples and exercises related to file handling and creating user-defined exceptions.

Uploaded by

milee1722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

PWP Notes 6

This document covers file input/output (I/O) handling and exception handling in Python. It explains how to perform various file operations such as opening, reading, writing, and managing files and directories, as well as the use of try-except blocks for handling exceptions. Additionally, it includes examples and exercises related to file handling and creating user-defined exceptions.

Uploaded by

milee1722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Unit-VI File I/O Handling and Exception Handling

6.1 I/O Operations: Reading keyboard input, Printing to screen

6.2 File Handling: Opening file in different modes, accessing file contents
using standard library functions, Reading and writing files, closing a file,
Renaming and deleting files, Directories in Python, File and directory related
standard functions

1. Opening a File in Different Modes:

Use the open() function to open a file in various modes:

●​ 'r': Read (default mode) – Opens the file for reading.


●​ 'w': Write – Opens the file for writing (creates a new file or overwrites an existing file).
●​ 'a': Append – Opens the file for appending (creates a new file if it doesn't exist).
2.Accessing file contents using standard library functions

In Python, the standard library functions are used to access file contents efficiently
Directories in Python
In Python, directories can be created, accessed, and managed using the os and os.path
modules.

Q.1.List different modes of opening file in python. - 2 mks

Q.2.With neat example differentiate between readline( ) and readlines( ) functions in


file-handling. - 4 mks

Q.3. Write a program to open a file in write mode and append


some contents at the end of file. - 6 mks
Q.4. WAP to read contents of first.txt file and write same content
in second.txt file. - 4 mks

# Open the first file in read mode and read its content
with open('first.txt', 'r') as first_file:
content = first_file.read()

# Open the second file in write mode and write the content to it
with open('second.txt', 'w') as second_file:
second_file.write(content)

print("Content has been copied from first.txt to second.txt.")

6.3 Exception Handling: Introduction, Exception handling - 'try: except:


statement, 'raise' statement, User defined exceptions
Q.1.Explain Try-except block used in exception handling in
python with example. - 6 mks

1. Introduction to Exception Handling

●​ Exceptions are errors that occur during program execution, such as dividing by zero or
accessing a file that doesn’t exist.
●​ If exceptions are not handled, the program terminates
2. Exception Handling Syntax

try-except Block

The try-except block is used to catch and handle exceptions.


4. User-Defined Exceptions
In Python, exceptions are used to handle errors that occur during the execution of a
program. While python provides many built-in exceptions, sometimes we may need to
create our own exceptions to handle specific situations that are unique to application.
These are called user-defined exceptions.

Creating User-Defined Exceptions


User-defined exceptions are created by defining a new class that inherits from
Python’s built-in Exception class or one of its subclasses. By doing this, we can create
custom error messages and handle specific errors in a way that makes sense for our
application.
Q.1.Write a Python program to create user defined exception that will check
whether the password is correct or not

You might also like