2. Need for
adata file
• T
o Store data in organized manner
• T
ostoredatapermanently
• T
oaccessdata faster
• T
o Searchdata faster
• T
oeasily modifydata lateron
3. File Handling
A file is a sequence of bytes on the disk/permanent storage where a
group of related data is stored. File is created for permanent storage of
data.
In programming, Sometimes, it is not enough to only display the data
on the console. Those data are to be retrieved later on,then the concept
of file handling comes. It is impossible to recover the programmatically
generated data again and again. However, if we need to do so, we may
store it onto the file system which is not volatile and can be accessed
every time. Here, comes the need of file handling in Python.
File handling in Python enables us to create, update, read, and delete
the files stored on the file system through our python program. The
followingoperationscan be performed ona file.
4. File Handling
Types of File
Thereare three types of files – Text File, Binary File and CSV file
Text Files- A file whose contents can be viewed using a text editor is called a text file. A
text file is simply a sequence of ASCII or Unicode characters. Python programs,
contents written in text editors are some of the example of text files.e.g. .txt,.rtf,.csv etc.
Binary Files-A binary file stores the data in the same way as as stored in the memory.
The .exe files,mp3 file, image files, word documents are some of the examples of binary
files.we can’t read a binary file using a texteditor.e.g. .bmp,.cdretc.
Text File Binary File
Its Bits represent character. Its Bits represent a custom data.
Less prone to get corrupt as change reflects
as soon as made and can be undone.
Can easily get corrupted, corrupt on even
single bit change
Store only plain text in a file.
Can store different types of data (audio,
text,image) in a single file.
Widely used file format and can be opened in
any text editor.
Developed for an application and can be
opened in that application only.
Mostly .txt,.rtf are used as extensions to text
files.
Can have any application defined extension.
5. File Handling
In Python, File Handling consistsof following threesteps:
Open the file.
Process file i.e perform read orwriteoperation.
Closethe file.
To Open a file
Syntax
fileobject=open(<file_name>,<access_mode>,< buffering>)
file_name = name of the file ,enclosed in double quotes.
access_mode= Determines thewhat kind of operationscan be
performed with file,likeread,writeetc.
Buffering = for no buffering set it to 0.forline buffering set it to 1.if it is
greater than 1 ,then it is buffer size.if it is negative then buffersize is
systemdefault.
To close a file
Syntax : fileobject.close()
6. File Handling
Fileopening modes-
SNo Mode & Description
1 r - reading only.Sets file pointer at beginning of the file . This is the default mode.
2 rb – same as r mode but with binary file
3 r+ - both reading and writing. The file pointer placed at the beginning of the file.
4 rb+ - same as r+ mode but with binary file
5 w - writing only. Overwrites the file if the file exists. If not, creates a new file for writing.
6 wb – same as w mode but with binary file.
7 w+ - both writing and reading. Overwrites . If no file exist, creates a new file for R & W.
8 wb+ - same as w+ mode but with binary file.
9 a -for appending. Move file pointer at end of the file.Creates new file for writing,if not exist.
10 ab – same as a but with binary file.
11 a+ - for both appending and reading. Move file pointer at end. If the file does not exist, it
creates a new file for reading and writing.
12 ab+ - same as a+ mode but with binary mode.
7. File Handling
Basic Text fileoperations
• Open ()
• Close a text file filename – absolute or
relative path, mode
• Reading/Writing data
• Manipulation of data
• Appending data intoa text file
8. File Handling
Methods of os module
1. The rename() method used torename the
file.
2.The remove() method todelete file.
Syntax os.remove(file_name)
2.The mkdir() method of theos module tocreate
directories in thecurrent directory.
Syntax os.mkdir("newdir")
4.The chdir() method tochange thecurrentdirectory.
Syntax os.chdir("newdir")
5.The getcwd() method displays thecurrentdirectory.
Syntax os.getcwd()
6. The rmdir() method deletes thedirectory.
Syntax os.rmdir('dirname')
e.g.program
Syntax os.rename(current_file_name, new_file_name) importos
print(os.getcwd())
os.mkdir("newdir")
os.chdir("newdir")
print(os.getcwd())
9. File Handling
Absolute Path vs Relative Path
One must be familiarwith absolute & relative path before starting file related operations.
Theabsolute path is the full path to some placeon yourcomputer. The relative path is the
path to some file with respect toyourcurrentworking directory (PWD). Forexample:
Absolute path: C:/users/admin/docs/staff.txt
If PWD is C:/users/admin/, then the relative path to staff.txtwould be: docs/staff.txt
Note, PWD + relativepath = absolutepath.
Cool, awesome. Now, if wewrite some scriptswhichcheck if a file exists.
os.chdir("C:/users/admin/docs")
os.path.exists("staff.txt")
This returnsTRUE if stuff.txt exists and it works.
Now, instead if we write,
os.path.exists("C:/users/admin/docs/staff.txt")
Thiswill returns TRUE.
If wedon't know wherethe userexecuting
the script from, it is best tocompute theabsolute
path on the user'ssystem using os and file .
file is a global variable set on every Python
script that returns the relative path to the *.py file
thatcontains it.
e.g.program
import os
print(os.getcwd())
os.mkdir("newdir1")
os.chdir("newdir1")
print(os.getcwd())
my_absolute_dirpath =
os.path.abspath(os.path.dirname( file ))
print(my_absolute_dirpath)
10. File Handling
Fileobjectattributes /
1. open a text file
closed: Itreturns true if the file is closed and false when the file is open.
encoding: Encoding used for bytestring conversion.
mode: Returns file opening mode
name: Returns the nameof the file which file object holds.
newlines: Returns “r”, “n”, “rn”, Noneora tuplecontaining all the
newlinetypes seen.
E.g. Program
f = open("a.txt", 'a+')
print(f.closed)
print(f.encoding)
print(f.mode)
print(f.newlines)
print(f.name)
OUTPUT
False
cp1252
a+
None
a.txt
11. File Handling
2. Closea text file
close(): Used toclose an open file. Afterusing this method,an
opened file will be closed and a closed file cannot be read or
written any more.
E.g. program
f = open("a.txt", 'a+')
print(f.closed)
print("Nameof the file is",f.name)
#2 close text file
f.close()
print(f.closed)
OUTPUT
False
Nameof the file is a.txt
True
12. File Handling
3. Read/write text file
Thewrite() Method
It writes the contents to the file in the form of string. It
does not return value. Due to buffering, the string may
not actually show up in the file until the flush() or
close() method is called.
The read() Method
It reads the entire file and returns it contents in the form
of a string. Reads at most size bytes or less if end of file
occurs.if size not mentioned then read the entire file
contents.
13. File Handling Read/Write a Text file
write() ,read() Method based program
def create():
f = open("a.txt", 'w')
line1 = 'Twinkle Twinkle Little Star'
f.write(line1) # Write data in a text file
line2="nHow I Wonder What You Are“
f.write(line2)
f.close()
def display():
f = open("a.txt", 'r')
text = f.read() # read data from a text file
print(text)
f.close()
create()
display()
OUTPUT
Twinkle Twinkle Little Star
How I Wonder What You are
14. File Handling - Read Text file
OUTPUT
Twinkle Twinkle Little Star
readline([size]) method: Read noof characters from file if size is mentioned till
Eof, read line till new line character, returnsempty string on EOF.
def display():
f = open("a.txt", 'r')
text = f.readline() # read one line at a time from a text file
print(text)
f.close()
display()
text = f.readline(5) will return Twink
15. File Handling
Read a Text File
def display():
f = open("a.txt", 'r')
for text in f.readlines(): # readlines() will read reamining data
print(text)
f.close()
Output:
Twinkle Twinkle Little Star
How I Wonder What You Are
16. File Handling - Read a Text file
Processing EveryWord in a File
def display():
f = open("a.txt", 'r')
for text in f.read().split():
print(text)
f.close()
display()
Output:
Twinkle
Twinkle
Little
Star
How
I
Wonder
What
You
Are
17. File Handling
Getting & Resetting the Files Position
The tell() method of python tells us the current position within the file,where as the
seek(offset[, from]) method changes the current file position. If from is 0 (Zero), the beginning
of the file to seek. If it is set to 1, the current position is used . If it is set to 2 then the end of the file
would be taken as seek position. Theoffset argument indicates the number of bytes to be moved.
e.g.program
f = open("a.txt", 'r')
print(f.tell())
print(f.read(7)) # read seven characters
print(f.tell())
print(f.read())
print(f.tell())
f.seek(9,0) # moves to 9 position from beginning
print(f.read(5))
f.seek(4) # moves to 4 position from current location
print(f.read(5))
f.close()
Output:
0
Twinkle
7
Twinkle Little Star
How I Wonder What You Are
53
winkl
kle T
18. File Handling
Binary file Operation
using pickle module
Python has a module which does this work for us and is
extremely easy to use. This module is called pickle; it provides
us with the ability to serialize and deserialize objects, i.e., to
convert objects into bitstreams which can be stored into files
and later be used to reconstruct theoriginal objects.
pickle.dump() function is used to store the object data to the file. It takes - First
argument is the object that we want to store. The second argument is the file object
we get by opening the desired file in write-binary (wb) mode.
Pickle.load() function is used to retrieve pickled data.The steps are quite simple.
Wehave to use pickle.load() function to do that. The primary argument of pickle
load function is the file objectthatyou get byopening the file in read-binary (rb)
mode
19. File Handling – Binary Files (Pickle mode)
import pickle
def create():
f=open("employee.dat","wb")
for x in range(5):
eno = int(input("Employee Number"))
ename = input("Name ")
dept = input("Dept ")
salary = int(input("Salary "))
R=[eno,ename,dept,salary]
pickle.dump(R,f) # pickle.dump(what,where)
f.close()
def display():
f=open("employee.dat","rb")
try:
while True:
R=pickle.load(f)
print(R)
except:
f.close()
20. File Handling
Search record in a Binary file
- pickle module
def empSearch(eno):
f=open("employee.dat","rb")
c=0
try:
while True:
R=pickle.load(f)
if R[0]==eno:
print(R)
c=1
break
except:
f.close()
if c==0:
print(eno," not found")
21. File Handling
Delete record of a Binary file
- pickle module
def delRecord(eno):
f1=open("employee.dat","rb")
f2=open("temp.dat","wb")
T,F=0,0
try:
while True:
R=pickle.load(f1)
T=T+1
if R[0]!=eno:
pickle.dump(R,f2)
F=F+1
except:
f1.close()
f2.close()
os.remove("employee.dat")
os.rename("temp.dat","employee.dat")
if T==F:
print(eno," not found")
Original file employee.dat is
opened in rb mode and temp.dat
file is opened in wb mode, assigned
to file objects f1 and f2 respectively.
Here we are reading all records
from binary file f1 and writing
those in f2 which are not to be
deleted.
Then employee.dat is removed by
os.remove() method and temp.dat
is renamed by using the method
os.rename().
22. Create a binary file student.dat which has a dict structure
with keys Rno, Name, avgMk and Grade. Input Rno,name
and avgMk, then Grade is calculated as
AvgMk Grade
>=75 A
>=50 and <75 B
>=33 and <50 C
<33 D
Input 5 records.
Read and Display the records scoring 60 marks and
above.
23. def create(): #Using a Dict
f=open("student.dat","wb")
for x in range(5):
rno=int(input("nRoll No:"))
name=input("Name:")
avgmk=float(input("Average Mark:"))
if avgmk>=75:
grade="A"
elif avgmk>=50:
grade="B"
elif avgmk>=33:
grade="C"
else:
grade="D"
d={'Rno':rno,'Name':name,'AvgMk':avgmk,"Grade":grade}
pickle.dump(d,f)
f.close()
24. def display60(fileName):
# to display records of all students scoring >=60
f=open(fileName,"rb")
c=0
# To keep the track of number of records >=60
try:
while True:
rec=pickle.load(f)
if rec['AvgMk']>=60: # if list is used rec[2]>=60:
print(rec)
c=c+1
except :
f.close()
print(fileName, “has “, c, “number of records>=60”)
display60("12A")
25. #To display data of lowest scorer
def displayHighest():
f=open("student.dat","rb")
M=0
R=[]
c=0
try:
while True:
rec=pickle.load(f)
if c==0:
M=rec['AvgMk']
R=rec
c=1
elif rec['AvgMk']<M:
M=rec['AvgMk']
R=rec
26. 1. Write a function to display data of lowest scorer
2. Write a function to display data of both highest
and lowest scorers
3. Update the student.dat file by increasing the
marks of D grade students by 3.
27. To update a file
1. open student.dat in rb mode and temp.dat in wb mode using
file objects f1 and f2 respectively.
2. Read data from f1 while not eof
if want to update, then edit the data
write to f2
else
write to f2
3. close both the files
4. remove student.dat
5. rename temp.dat as student.dat
6. stop
28. File Handling – csv files (Comma Separated Value files)
CSV (Comma Separated Values) is a file format for data storage which looks
likea text file. The information is organized withone record on each line and
each field is separated bycomma.
CSV File Characteristics
• One line foreach record
• Commaseparated fields
• Space-characters adjacenttocommasare ignored
• Fields with in-builtcommasare separated bydoublequotecharacters.
When Use CSV?
• When data has a strict tabularstructure
• Totransfer largedatabase between programs
• Toimportand exportdata tooffice applications, Qedoc modules
• Tostore, manageand modifyshopping cartcatalogue
29. CSVAdvantages
• CSV is fasterto handle
• CSV is smaller in size
• CSV is easy togenerate
• CSV is human readableand easy toedit manually
• CSV is simple to implementand parse
• CSV is processed byalmostall existing applications
CSV Disadvantages
• Nostandard way torepresent binarydata
• There is nodistinction between textand numericvalues
• Poorsupportof special characters and control characters
• CSV allows to move most basicdataonly. Complexconfigurationscannot
be imported and exported thisway
• Problemswith importing CSV into SQL (nodistinction between NULL
and quotes)
File Handling – csv files (Comma Separated Value files)
30. File Handling – write / read csv file
importcsv
#csv file writingcode
F = open('a.csv','w')
F1 = csv.writer(F)
F1.writerow(['user_id','beneficiary'])
F1.writerow([1,'xyz'])
F1.writerow([2,'pqr'])
F.close()
To read data from a csv file
with open('a.csv','r') as F:
F1 = csv.reader(F)
for row in F1:
print (row)
F.close()
First of all we have to import csv
module for file operation/method
call.
For writing, we open file in ‘w’
writing mode using open() method
whichcreate F likeobject.
Through csv
.writer() method, we
create a writer object F1 to
writerow() method to writeobjects.
Similarly for reading ,we open the
file in ‘r’ mode and create newFile
like object, further we create a
newfilereader object using
csv.reader() method to read each row
of the file.
31. Practice Questions
Text file
Q1 Create a text file story.txt and perform the followings:
• To display each word and number of words in the text file.
• To display
• Number of uppercase alphabets
• Number of lowercase alphabets
• Number of vowels
• Number of digits
• Number of blank spaces present in the file.
• To display reverse of each word starting with an uppercase alphabet
• To display the frequency of each alphabet present in the file
32. Practice Questions
Q2. Write functions to:
•Create a binary file “Student.dat” which has record structure:
Rno, Name, A list containing marks in 5 subjects,
Total Marks and Average Marks
Input Rno, name and marks in 5 subjects, assign Total and Average.
•Display all data and Result for each student. (Result is Pass if Average >=33%
and Fail, otherwise)
Q3. Write functions to:
a) Create a binary file “Employee.dat” which has record structure:
Employee name, employee number, Dept and Salary.
b) Display all records.
c) Calculate and return total salary paid to all employees
d) To pass an employee number and display all data, if employee number is
found, Error message, otherwise.
e) To display data of employees whose salary >=50000, also print number of
such employees.
f) In the Employee.dat file, enter an employee number and delete the
record, if found, otherwise display error message.