File Handling in Python
File Handling in Python
File : A file is a named location on a secondary storage media where data are
permanently stored for later access. Computers store every file as a collection
of 0s and 1s i.e., in binary form. Therefore, every file is basically just a series of
bytes stored one after the other.
TYPES OF FILES :
There are mainly two types of data files — text file and binary file.
Note :
1. Trying to open a binary file using a text editor will show some garbage
values.
2. When a text editor or a program interpreter encounters the ASCII
equivalent of the EOL character, it displays the remaining file contents
starting from a new line.
3. Contents in a text file are usually separated by whitespace, but comma
(,) and tab (\t) are also commonly used to separate values in a text file.
Operation on Files:
We broadly access files either to write or read data from it. But operations on
files include creating and opening a file, writing data in a file, traversing a file,
reading data from a file and so on. Python has the io module that contains
different functions for handling files.
1. Opening a File :
To open a file in Python, we use the open() function. The syntax of open()
is as follows:
open() function returns a file object called file handle which is stored in
the variable file_object.
We can use this variable to transfer data to and from the file (read and
write) by calling the functions defined in the Python’s io module.
The file Handle has certain attributes that tells us basic information
about the file. Some of them are as follow :
• <file.closed> returns true if the file is closed and false otherwise.
• <file.mode> returns the access mode in which the file was opened.
• <file.name> returns the name of the file.
The file_name should be the name of the file that has to be opened. If
the file is not in the current working directory, then we need to specify
the complete path of the file along with its name.
The access_mode is an optional argument that represents the mode
in which the file has to be accessed by the program. It is also referred
to as processing mode. Mode means the operation for which the file
has to be opened like <r> for reading, <w> for writing, <+> for both
reading and writing, <a> for appending at the end of an existing file.
NOTE :
Example :
2. Closing a file :
Python provides a close() method to do so. While closing a file, the system
frees the memory allocated to it. The syntax of close() is:
file_object.close()
file_object is the object that was returned while opening the file.
Note :
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.
If the file object is re-assigned to some other file, the previous file
is automatically closed.
1. 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.
2. In case the user forgets to close the file explicitly or if an exception
occurs, the file is closed automatically.
3. Also, it provides a simpler syntax.
with open(“myfile.txt”,”r+”) as myObject:
content = myObject.read()
Syntax : file_object.write(<String>)
write() method takes a string as an argument and writes it to the text file.
It returns the number of characters being written on single execution of
the write() method.
Also, we need to add a newline character (\n) at the end of every
sentence to mark the end of line.
Consider the following piece of code:
>>> myobject=open("myfile.txt",'w')
>>> myobject.write("Hey I have started #using files in Python\n")
45
>>> myobject.close()
On execution, write() returns the number of characters written on to the file.
Hence, 41, which is the length of the string passed as an argument, is
displayed.
For example:
>>>myobject=open("myfile.txt",'w')
>>> marks=58 #number 58 is converted to a string using #str()
>>> myobject.write(str(marks))
2
>>>myobject.close()
The write() actually writes data onto a buffer. When the close() method is
executed, the contents from this buffer are moved to the file located on the
permanent storage.
>>>myobject=open("myfile.txt",'r')
>>> print (myobject.readline())
'Hello everyone\n'
To read the entire file line by line using the readline(), we can use a
loop. This process is known as looping/ iterating over a file object.
It returns an empty string when EOF is reached.
(iii) 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.
Q. write a program that accepts a string from the user and writes it to a text
file. Thereafter, the same program reads the text file and displays it on the
screen.
Ans.
def writedata():
file_object.seek(offset [, reference_point])
In the above syntax, offset is the number of bytes by which the
file object is to be moved.
reference_point indicates the starting position of the file object.
That is, with reference to which position, the offset has to be
counted. It can have any of the following values:
0 - beginning of the file
1 - current position of the file
2 - end of file
By default, the value of reference_point is 0, i.e. the offset is
counted from the beginning of the file. For example, the statement
fileObject.seek(5,0)
will position the file object at 5th byte position from the beginning
of the file.
Output of Program :
Learning to move the file object
roll_numbers = [1, 2, 3, 4, 5, 6]
Initially, the position of the file object is: 33
Now the file object is at the beginning of the file: 0
We are moving to 10th byte position from the beginning of file
The position of the file object is at 10
numbers = [1, 2, 3, 4, 5, 6]
We know that 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.
Likewise, you may like to store a Python dictionary as an object, to be able to
retrieve later.
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.
where data_object is the object that has to be dumped to the file with the file
handle named file_OBJECT.
Example :
import pickle
listvalues=[1,"Geetika",'F', 26]
fileobject=open("mybinary.dat", "wb") pickle.dump(listvalues,fileobject)
fileobject.close()
Here, the pickled Python object is loaded from the file having a file handle
named file_object and is stored in a new object called store_object.
Example :
import pickle
print("The data that were stored in file are: ")
fileobject=open("mybinary.dat","rb")
objectvar=pickle.load(fileobject)
fileobject.close()
print(objectvar)
Q Program to read the employee records from the file using load() module
Note :
As each employee record is stored as a list in the file empfile.dat, hence while
reading the file, a list is displayed showing record of each employee.
Q. Write a method countvowels() that reads a text file DIARY.TXT and count
the number of vowels in the file and display it.
Contents of text file "DAIRY.TXT" :
This is my Website.
My name is Parveen.
Ans.
def countvowels() :
file = open("DIARY.TXT", "r")
S = file.read()
v=0
for ch in S :
if ch in 'aAeEiIoOuU': # if ch in [‘a’, ‘e’,’i’, ‘o’,’u’, ‘A’,’E’,’I’,’O’,’U’]
v = v +1
file.close()
print("Number of vowels : ", v )
#------------------------------------------------
OR
def countvowels() :
file = open("DIARY.TXT", "r")
S = file.read()
v=0
for i in range(len(S)) :
if S[i] in 'aAeEiIoOuU': # if S[i] in [‘a’, ‘e’,’i’, ‘o’,’u’, ‘A’,’E’,’I’,’O’,’U’]
v = v +1
file.close()
print("Number of vowels : ", v )
def countlines():
file = open("DAIRY.XT", "r")
line = file.readline()
cnt = 0
while line :
if line[0] == 'A' or line[0] == 'a' : # if line[0] in ‘aA’ :
cnt = cnt +1
line = file.readline()
print("Number of lines = ", cnt)
file.close()
Q. Write python program to create a binary file storing the list of marks
obtained by different students in computer science. Program then read
the file and display the marks obtained by them.
Ans.
def creatbinary() :
file = open("Marks.dat", "wb")
list1 = []
while True :
M = float(input("Enter Marks : "))
list1.append(M)
ch = input("Wish to enter More Marks : ")
if ch in ‘nN’ :
break
pickle.dump(list1,file)
file.close()
#----------------------------------------------------------------------
def readbinaryfile() :
file = open("Marks.dat", 'rb')
try :
while True :
list1 = pickle.load(file)
print(list1)
except EOFError :
pass
file.close()
#--------------------------------------------------------------------------------
OR
def createfile():
file = open("Marks.dat", "wb")
d1 = dict()
while True :
nm = input("Enter Name : ")
M = float(input("Enter the marks in CS : "))
d1[nm] = M
ch = input("Wish to enter More Marks (Y/N): ")
if ch in "nN" :
break
pickle.dump(d1, file)
file.close()
#----------------------------------------------------------------
def readfile():
file = open("Marks.dat", "rb")
try :
while True :
d1 = pickle.load(file)
print(d1)
except EOFError :
pass
file.close()
CSV FILE :
Data sharing is one of the major tasks to be carried out , largely through
spreadsheets or databases. A basic approach to share data is through the
comma separated values (CSV) files.
CSV is a simple flat text file in human readable format which is extensively
used to store tabular data in a spreadsheet or database.
Files in the CSV format can be imported to or exported from programs that
store data in tables.
Each line in CSV file is known as record. Each record consists of one or more
fields separated by commas (known as delimiters).
Example : marks.csv
Name,Class,Marks
Manish,12,90
Aman,12,92
Himanshu,12,65
(i) The extensive use of social networking sites and their various
associated applications requires the handling of huge data. The
processing data have complex structure. The CSV organizes data
into a structured form. Hence proper and systematic organization
of large amount of data is done by CSV.
(ii) It makes very easy for Website developers to create applications that
implements CSV.
(iii) CSV files are commonly used as they are easy to read and manage,
small in size and fast to process/transfer.
Syntax :
<csv_writer_object> = csv.writer(file_object, delimiter = ‘,’)
(b) After creating the writer object, write the user’s data into
CSV file by using writerow() function or writerows()
function.
Syntax :
<csv_writer>.writerow(<iterable>)
(i) When a CSV file is opened in read/write mode then we have to passed
newline argument as “” (Empty String)
By default newline argument has value ‘\r\n’ and therefore , we have
a blank record between two records. To avoid this, it is necessary to
passed newline argument as “”.
(ii) To write multiple records in one go into a CSV file , we can use
writerows() function of csv module.
Example :
def createcsv():
f = open("marks.csv", "w", newline = '')
csv_w = csv.writer(f,delimiter = ',') # creating a writer object using file
handle ‘f’
fields = ['Name', "class", "Marks"]
csv_w.writerow(fields) # writing a delimited string or record
in the csv file
while True :
nm = input ("Enter Name : ")
cl = input("Enter Class : ")
M = float(input("Enter Marks : "))
rec = [nm, cl, M]
csv_w.writerow(rec) # pickle.dump(rec,f) for binary file
ch = input("Wish to write more records (Y/N) : ")
if ch in 'nN' :
break
f.close()
print("File Created")
def readcsv():
f = open("marks.csv", "r", newline ='')
records = csv.reader(f)
for r in records :
#print(r)
print(','.join(r))
f.close()
#------ main----------------
import csv
createcsv()
readcsv()
OR
def createcsv():
f = open("marks.csv", "w", newline = '')
csv_w = csv.writer(f,delimiter = ',')
fields = ['Name', "class", "Marks"]
csv_w.writerow(fields)
records = list()
while True :
nm = input ("Enter Name : ")
cl = input("Enter Class : ")
M = float(input("Enter Marks : "))
rec = [nm, cl, M]
records.append(rec)
ch = input("Wish to write more records (Y/N) : ")
if ch in 'nN' :
break
csv_w.writerows(records)
f.close()
print("File Created")
def readcsv():
f = open("marks.csv", "r", newline ='')
records = csv.reader(f)
for r in records :
#print(r)
print(','.join(r))
f.close()
#------ main----------------
import csv
createcsv()
readcsv()
Q. Write a menu driven program that will perform the following tasks :
1. Add a record into a binary file
2. Display all Records
3. Search a record
4. Delete a Record
5. Update a Record
Ans.
""" Menu Driven program to add records in a binary file,
display the records, Search a record, Update a record
and delete a record.
"""
#-------------- Function to add records -------------
def addrecord():
file = open("stud.dat", "ab")
while True :
rn = int(input("Enter the roll number : "))
nm = input("Enter the name : ")
marks = float(input("Enter the marks : "))
rec = [rn, nm, marks] # created a record
pickle.dump(rec,file)
ch = input("Wish to add records(Y/N) : ")
if ch in "Nn" :
break
file.close()
#--------------- Function to display records --------
def displayrec() :
file = open("stud.dat","rb")
print("Rollno\t", "Name\t", "Marks")
try :
while True :
rec = pickle.load(file)
print(rec[0],rec[1],rec[2], sep = "\t")
except EOFError :
pass
file.close()
#----------------------Search a record ---------------
def searchrec():
R = int(input("Enter the roll number : "))
file = open("stud.dat", "rb")
found = 0
try :
while True :
rec = pickle.load(file)
if rec[0] == R :
found = 1
break
except EOFError :
pass
if found == 1 :
print( "Roll Number : ", rec[0])
print(" Name : ", rec[1])
print(" Marks : ", rec[2])
else :
print("Record not found!!!! ")
file.close()
#-----------------------Main Block---------------------
import pickle
while True :
menu()
ch = int(input("Enter your Choice : "))
if ch == 1 :
addrecord()
elif ch == 2 :
displayrec()
elif ch == 3 :
searchrec()
elif ch == 4 :
modifyrec()
elif ch == 5 :
deleterec()
elif ch == 6 :
print("Good Bye!!! ")
break
else :
print("Invalid Choice ")