Python File Handling
Python File Handling
Samir V
Two types of files can be handled in Python, normal text files and binary files
(written in binary language, 0s, and 1s).
Text files: In this type of file, Each line of text is terminated with a special
character called EOL (End of Line), which is the new line character (‘\n’) in
Python by default.
A regular text file can be understood as a sequence of characters consisting of
alphabets, numbers and other special symbols. These files are with extensions
like .txt, .py etc
Few text file contents are usually separated comma (,) or tab (\t).Thse files are
called csv (comma separated value) or tsv(tabs seperated value) respectively
Binary files: In this type of file, there is no terminator for a line, and the data is
stored after converting it into machine-understandable binary language.
Binary files are stored in a computer in a sequence of bytes. Even a single bit
change can corrupt the file and make it unreadable to the supporting
application.
Binary files operations are faster than text file as it is deliminator free hence
translation is not required.
slide 2
Samir V
In Python, there are six methods or access modes, which are:
Read Only ('r’): This mode opens the text files for reading only. It raises the I/O
error if the file does not exist. This is the default mode for opening files as well.
Read and Write ('r+’): This method opens the file for both reading and writing. If
the file does not exist, an I/O error gets raised.
Write Only ('w’): This mode opens the file for writing only. The data in existing files
are overwritten. If the file does not already exist in the folder, a new one gets
created.
Write and Read ('w+’): This mode opens the file for both reading and writing. The
text is overwritten and deleted from an existing file.
Append Only ('a’): This mode allows the file to be opened for writing. If the file
doesn't yet exist, a new one gets created. The newly written data will be added at
the end, following the previously written data.
Append and Read (‘a+’): Using this method, you can read and write in the file. If the
file doesn't already exist, one gets created. The newly written text will be added at
the
slide 3 end.
Samir V
Working with Text Files
slide 4
Samir V
Opening a file
To open a file in Python, we use the open() function. The syntax of open() is
as follows:
file_object= open(file_name, access_mode)
This function returns a file object called file handle which is stored in the
variable file_object. This file handle is used to transfer data to and from the
file (read and write).
The file handle has certain attributes that tells us basic information about
the file, such as: file location, mode, capacity, last accessed position,
metadata etc.
If the file does not exist, the above statement creates a new empty file and
assigns it the name we specify in the statement.
If you do not want to add double slashes, python provide alternate way-
One need to r preceding the file path. This tells interpreter to treat the path
as raw string.
slide 6
Samir V
Closing a file
Here, file_object is the object that was returned while opening the file.
Python makes sure that any unwritten or unsaved data is flushed off
(written) to the file before it is closed. Hence, it is always advised to
close the file once our work is done.
file1 = open("MyFile.txt","a")
….
….
file1.close()
slide 7
Samir V
WRITING TO A TEXT FILE
For writing to a file, we first need to open it in write or append mode. If we open
an existing file in write mode, the previous data will be erased, and the file
object will be positioned at the beginning of the file.
On the other hand, in append mode, new data will be added at the end of the
previous data as the file object is at the end of the file.
After opening the file, we can use the following methods to write data in the file.
WE CAN WRITE ONLY STRING DATA TO TEXT FILES.
myobject=open("myfile.txt",'w’)
marks=58
#number 58 is converted to a string using str()
myobject.write(str(marks))
>>> 4
slide 9
Samir V
• writelines() - This method is used to write multiple strings to a file. We need to
pass an iterable object like lists, tuple, etc.
file1.writelines(lst)
file1.close()
print("Data is written into the file.")
Unlike write(), the writelines() method does not return the number of characters
written in the file.
slide 10
Samir V
The flush() method flushes the internal buffer. This internal buffer is
maintained to speed up file operations.
# Open a file
fo = open("foo.txt", "wb")
fo.flush()
Samir V
READING FROM A TEXT FILE
We can write a program to read the contents of a file. Before reading a file,
we must make sure that the file is opened in “r”, “r+”, “w+” or “a+” mode.
There are three ways to read the contents of a file:
myobject=open("myfi le.txt",'r')
myobject.read(10)
myobject.close()
This method reads one complete line from a file where each line terminates
with a newline (\n) character. Itcan also be used to read a specified number
(n) of bytesof data from a file but maximum up to the newline
character (\n).
In the following example, the second statement reads the first ten
characters of the first line of the text file and displays them on the screen.
myobject=open("myfile.txt",'r')
myobject.readline(10)
myobject.close()
myobject=open("myfi le.txt",'r')
print (myobject.readline())
To read the entire file line by line using the readline(), we can use a loop.
Samir V
The readlines() method
The method reads all the lines and returns the lines along with newline as a
list of strings. The following example uses readlines() to read data from the
text file myfile.txt.
Exercise -
Write a program that accepts a conversation from two user and writes it to a
text file. Thereafter, the same program reads the text file and displays it on
the screen.
Samir V
Opening a file using with clause:
The advantage of using with clause is that any file that is opened using this
clause is closed automatically, once the control comes outside the with
clause. In case the user forgets to close the file explicitly or if an exception
occurs, the file is closed automatically.
Here, we don’t have to close the file explicitly using close() statement.
Python will automatically close the file.
Samir V
Working with Binary Files
Samir V
Python considers everything as an object. So, all data types including list,
tuple, dictionary, etc. are also considered as objects.
During execution of a program, we may require to store current state of
variables so that we can retrieve them later to its present state. Suppose you
are playing a video game, and after some time, you want to close it. So, the
program should be able to store the current state of the game, including
current level/stage, your score, etc. as a Python object.
To save any object structure along with data, Python provides a module
called Pickle. The module Pickle is used for serializing and de-serializing any
Python object structure.
Samir V
Why Do We Need Object Serialization?
students = {
'Student 1’: { 'Name': "Alice", 'Age' :10, 'Grade':4, },
Samir V
let’s proceed to write it to a text file without serialization:
The pickle module provides two methods - dump() and load() to work with
binary files for pickling and unpickling, respectively
Samir V
The dump() method
This method is used to convert (pickling) Python objects for writing data in a
binary file. The file in which data are to be dumped, needs to be opened in
binary write mode (wb).
where data_object is the object that has to be dumped to the file with the
file handle named file_ object.
Following program writes the record of a student (roll_no, name, gender and
marks) in the binary file named mybinary.dat using the dump(). We need to
close the file after pickling
import pickle
listvalues=[1,"Geetika",'F', 26]
fileobject=open("mybinary.dat", "wb")
pickle.dump(listvalues , fileobject)
fileobject.close()
The load() method raises EOFError when it reaches end of file. Hence load()
method must be enclosed in try … except block.
import pickle
fileobject=open("mybinary.dat","rb")
try:
objectvar=pickle.load(fileobject)
#perform other operations
except EOFError:
fileobject.close()
Samir V
import pickle
list= [1,2,3,4]
list1= [11,2,3,4]
list2= [12,2,3,4]
list3= [13,2,3,4]
Appending data to binary file is same as text file with difference that file
need to opened using ‘ab’ option.
Once opened, we can use normal dump() function to append data to a file.
Samir V
Exercise – Write a program to enter Employee name, age, gender, Salary and
date of joining.
Program should get the data from user and write to file(Emp.dat) in binary
mode until User wishes. Display all data by reading a file.
Samir V
SETTING OFFSETS IN A FILE
tell() method
This function returns an integer that specifies the current position of the file
object in the file. The position specified is the byte position from the
beginning of the file.
The syntax of using tell() is:
Poition = file_object.tell()
Samir V
seek() method
This method is used to position the file object at a particular position in a file.
The syntax of seek() is:
file_object.seek(offset [,mode])
Samir V
print("Learning to move the file object")
fileobject=open("testfile.txt","r+")
str=fileobject.read()
print(str)
print("Initially, the position of the file object is: ",fileobject. tell())
fi leobject.seek(0)
print("Now the fi le object is at the beginning of the file: ",fileobject.tell())
fileobject.seek(10) # fileobject.seek(10,0)
print("We are moving to 10th byte position from the beginning of file")
print("The position of the file object is at", fileobject.tell())
str=fileobject.read(5)
print(str)
fileobject.seek(20,1)
print("We are moving to 20th position from the current position")
print("The position of the file object is at", fileobject.tell())
Samir V
Searching in a file- There is no any pre-defined function available in python
for searching records in binary file in python. We will define our logic or
function to solve our problem of updating records.
import pickle
list= [[1,2,3,4],[11,22,33,44],[21,32,43,54],[91,92,93,94]]
pickle.dump(list, file)
file.close()
Samir V
try:
while True:
l1 = pickle.load(file)
num=int(input("Enter number to search"))
for l1 in list:
for j in l1:
if j == num:
found = True
except EOFError:
file.close
if (found):
print("number Present")
else:
print("number not Present")
Samir V
Binary File updation – A company want to give bonus to its employees.
Write a program to distribute bonus using following data.
The file is opened in “rb+” mode which is for reading as well as for writing.
Write all data to file. Make sure you have at least 3-4 entries.
We will use seek() and tell() functions to modify file pointer.
import pickle
list1= [1111,2222,33333,4444]
list2 = [1122,2233,3344,4455]
list3 = [2121,3232,4343,5454]
list4 = [9192,9293,9394,9495]
pickle.dump(list1, file)
pickle.dump(list2, file)
pickle.dump(list3, file)
pickle.dump(list4, file)
file.close()
Samir V
file =open("myfile.dat", 'rb+')
found = False
try:
while True:
pos= file.tell()
record = pickle.load(file)
# print(record)
for l1 in record:
if l1 == num:
found = True
ind= record.index(l1)
num1= int(input("enter number to replace"))
record[ind] =num1
#print(record)
file.seek(pos,0)
pickle.dump(record,file)
break;
Samir V
except EOFError:
file.close()
if (found):
print("record updated")
else:
print("number not Present")
import pickle
l1 = pickle.load(file1)
print(l1)
except EOFError:
file1.close()
Samir V
Exercise – A company want to give bonus to its employees.
Write a program to distribute bonus using following data.
Years Completed Bonus %
0 <= 5 No Bonus
> 5 <= 8 10% of salary
>8 15 % of salary
Samir V
Summary –
• readlines() method reads all the lines and returns the lines along with
newline character, as a list of strings.
• tell() method returns an integer that specifies the current position of the
file object. The position so specified is the byte position from the
beginning of the file till the current position of the file object.
• seek()method is used to position the file object at a particular position in
a file.
Samir V