Unit - III
Unit - III
Unit - IV
UNIT III
FILE HANDLING
Python File Input-Output:
File input-output in Python is the process of reading from and writing to files on your computer's
storage. This is essential for handling data persistence and manipulation. Here are the basic steps:
1. Opening a File:
Use the open() function to open a file.
To perform any operation on a file, you first need to open it using the open() function.
Syntax:
file_object = open(file_path, mode).
i. file_path:
refers to the location or path of the file on your computer's filesystem. It includes the directory
or folder where the file is located, along with the file's name and extension. For example, in
the path "D:\Programs\Python\example.txt",
D:\Programs\Python\ is the directory and example.txt is a file
ii. mode:
in file handling specifies the purpose for which a file is opened. There are different modes
like:
1. 'r' (Read):
This mode allows only reading from the file.
If the file does not exist, it raises a FileNotFoundError.
It is the default mode when opening a file.
2. 'w' (Write):
This mode allows writing to the file. If the file exists, it will overwrite (i.e., erase all data)
and start writing from the beginning.
If the file does not exist, it will create a new one.
3. 'a' (Append):
This mode allows appending data to the end of the file.
Page 1 of 8
By, Mritunjay Kr. Ranjan
Python Programming
Unit - IV
If the file exists, it will not overwrite and start writing from the end.
If the file does not exist, it will create a new one.
2. Writing to a File:
i. Open the File:
Use the open() function to open the file in the desired mode
(in this case, "w" for write).
Page 2 of 8
By, Mritunjay Kr. Ranjan
Python Programming
Unit - IV
Where the texts will be inserted depends on the file mode and stream position.
file.close()
content = file.read()
ii. readline():
readline() is used to read a single line from the file.
line = file.readline()
iii. readlines():
readlines() is used to read all lines of the file into a list where each line is an element of
the list.
lines = file.readlines()
4. Closing a File:
It's important to close a file after you're done with it to free up system resources.
Use the close() method.
Program
Page 3 of 8
By, Mritunjay Kr. Ranjan
Python Programming
Unit - IV
Page 4 of 8
By, Mritunjay Kr. Ranjan
Python Programming
Unit - IV
Example of writelines():
Page 5 of 8
By, Mritunjay Kr. Ranjan
Python Programming
Unit - IV
In this file format, the data is stored in the binary format (1 or 0). The binary file doesn't have any terminator
for a newline. Python takes the three required steps to read or write a text file.
Open a file
Read or Write file
Close file
Page 6 of 8
By, Mritunjay Kr. Ranjan
Python Programming
Unit - IV
seek() method
In Python, seek() function is used to change the position of the File Handle to a given specific
position. File handle is like a cursor, which defines from where the data has to be read or written in
the file.
Page 7 of 8
By, Mritunjay Kr. Ranjan
Python Programming
Unit - IV
The tell() method returns the current file position in a file stream.
file.tell()
Example:
f = open("demofile.txt", "r") Output:
Hello! Welcome to demofile.txt
print(f.readline()) 32
print(f.tell())
******
Page 8 of 8
By, Mritunjay Kr. Ranjan