File Handling Complete Notes With Programs
File Handling Complete Notes With Programs
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.
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 text editor.
pg. 1
used as application defined
extensions to text files. extension
Opening and Closing FilesTo perform file operation ,it must be opened first
then after reading ,writing, editing operation can be performed. To create any
new file then too it must be opened. On opening of any file ,a file relevant
structure is created in memory as well as memory space is created to store
contents. Once we are done working with the file, we should close the file.
Closing a file releases valuable system resources. In case we forgot to close
the file, Python automatically close the file when program ends or file object is
no longer referenced in the program. However, if our program is large and we
are reading or writing multiple files that can take significant amount of
resource on the system. If we keep opening new files carelessly, we could run
out of resources. So be a good programmer , close the file as soon as all task
are done with it.
open Function :Before any reading or writing operation of any file,it must be
opened firstof all. Python provide built in function open() for it. On calling of
this function creates file object for file operations.
Syntax
file object = open(<file_name>, <access_mode>,< buffering>)
file_name = name of the file ,enclosed in double quotes.
access_mode= Determines the what kind of operations can be performed with
file,likeread,write etc.
Buffering = for no buffering set it to 0.for line buffering set it to 1.if it is
greater than 1 ,then it is buffer size.if it is negative then buffer size is system
default.
File opening modes
Sr.
No Mode & Description
.
r - readingonly.Sets file
pointer at beginning of
1
the file . This is the
default mode.
rb– same as r mode
2
but with binary file
r+ - both reading and
writing. The file
3
pointer placed at the
beginning of the file.
rb+ - same as r+ mode
4
but with binary file
w - writing only.
5
Overwrites the file if
pg. 2
the file exists. If not,
creates a new file for
writing.
wb– same as w mode
6
but with binary file.
w+ - both writing and
reading. Overwrites . If
7
no file exist, creates a
new file for R & W.
wb+ - same as w+
8 mode but with binary
file.
a -for appending. Move
file pointer at end of
9 the file.Creates new
file for writing,if not
exist.
ab– same as a but with
10
binary file.
a+ - for both
appending and
reading. Move file
pointer at end. If the
11
file does not exist, it
creates
a new file for reading
and writing.
ab+ - same as a+ mode
12
but with binary
E.g. Program
f = open("a.txt", 'a+')
print(f.closed)
print(f.encoding)
pg. 3
print(f.mode)
print(f.newlines)
print(f.name)
OUTPUT
False
cp1252
a+
None
a.txt
The close() Method
close(): Used to close an open file. After using 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("Name of the file is",f.name)
f.close()
print(f.closed)
OUTPUT
False
Name of the file is a.txt
True
The write() 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.
pg. 4
text = f.read()
print(text)
f.close()
OUTPUT
Welcome to python.mykvs.in
Regularly visit python.mykvs.in
readline([size]) method: Read no of characters from file if size is mentioned
till eof.read line till new line character.returns empty string on EOF.
e.g. program
f = open("a.txt", 'w')
line1 = 'Welcome to python.mykvs.in'
f.write(line1)
line2="\nRegularly visit python.mykvs.in"
f.write(line2)
f.close()
f = open("a.txt", 'r')
text = f.readline()
print(text)
text = f.readline()
print(text)
f.close()
OUTPUT
Welcome to python.mykvs.in
Regularly visit python.mykvs.in
readlines([size]) method: Read no of lines from file if size is mentioned or all
contents if size is not mentioned.
e.g.program
f = open("a.txt", 'w')
line1 = 'Welcome to python.mykvs.in'
f.write(line1)
line2="\nRegularly visit python.mykvs.in"
f.write(line2)
f.close()
f = open("a.txt", 'r')
text = f.readlines(1)
print(text)
f.close()
pg. 5
OUTPUT
['Welcome to python.mykvs.in\n']
NOTE – READ ONLY ONE LINE IN ABOVE PROGRAM.
Iterating over lines in a file
e.g.program
f = open("a.txt", 'w')
line1 = 'Welcome to python.mykvs.in'
f.write(line1)
line2="\nRegularly visit python.mykvs.in"
f.write(line2)
f.close()
f = open("a.txt", 'r')
for text in f.readlines():
print(text)
f.close()
Processing Every Word in a File
e.g.program
f = open("a.txt", 'w')
line1 = 'Welcome to python.mykvs.in'
f.write(line1)
line2="\nRegularly visit python.mykvs.in"
f.write(line2)
f.close()
f = open("a.txt", 'r')
for text in f.readlines():
for word in text.split( ):
print(word)
f.close()
OUTPUT
Welcome
to
python.mykvs.in
Regularly
visit
python.mykvs.in
Append content to a File
f = open("a.txt", 'w')
line = 'Welcome to python.mykvs.in\nRegularly visit python.mykvs.in'
f.write(line)
f.close()
pg. 6
f = open("a.txt", 'a+')
f.write("\nthanks")
f.close()
f = open("a.txt", 'r')
text = f.read()
print(text)
f.close()
OUTPUT
Welcome to python.mykvs.in
Regularly visit python.mykvs.in
thanks
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,
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. The offset
argument indicates the number of bytes to be moved.
e.g.program
f = open("a.txt", 'w')
line = 'Welcome to python.mykvs.in\nRegularly visit python.mykvs.in'
f.write(line)
f.close()
f = open("a.txt", 'rb+')
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 begining
print(f.read(5))
f.seek(4, 1) # moves to 4 position from current location
print(f.read(5))
f.seek(-5, 2) # Go to the 5th byte before the end
print(f.read(5))
f.close()
OUTPUT
0
b'Welcome'
7
b' to
pg. 7
python.mykvs.in\r\n
Regularly visit
python.mykvs.in'
59
b'opyt'
b'mykvs'
b'vs.in'
Methods of os module
e.g.program
import os
print(os.getcwd())
os.mkdir("newdir")
os.chdir("newdir")
print(os.getcwd())
File Handling
pg. 8
If PWD is C:/users/admin/, then the relative path to staff.txt wouldbe:
docs/staff.txt
Note, PWD + relative path = absolute path.
os.chdir("C:/users/admin/docs")
os.path.exists("staff.txt")
This returns TRUE if stuff.txt exists and it works.
Now, instead if we write,
os.path.exists("C:/users/admin/docs/staff.txt")
This will returns TRUE.
If we don't know where the user executing the script from, it is best to
compute the absolute path on the user's system using os and __file__.
__file__ is a global variable set on every Python script that returns the relative
path to the *.py filethat contains 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)
Copying the contents of one text file to another
a=open("tocopy.txt","r")
x=a.read()
b=open("copy.txt","w")
b.write(x)
print("copied")
a.close()
b.close()
pg. 9
Entering data segregated with colon
count=int(input("Enter the number of students"))
f=open("separator1","a")
fori in range(count):
Roll=int(input("Enter the rollno"))
Name=input("Enter the name of the student")
Mark=int(input("Enter the mark"))
rec=str(Roll)+":"+Name+":"+str(Mark)+"\n"
f.write(rec)
print(rec)
f.close()
f=open("separator1","r")
for line in f:
cols=line.split(":")
x=cols[0]
y=cols[1]
z=cols[2]
print(x,y,z)
f.close()
pg. 10
count2=count2+1
print("The number of lowercase characters is:")
print(count1)
print("The number of uppercase characters is:")
print(count2)
open("d:/mytext.txt","r")
str1=file1.read()
vowel_count=0
foriinstr1:
if(i=='A'ori=='a'ori=='E'ori=='e'ori=='I'
ori=='i'ori=='O'ori=='o'
ori=='U'ori=='u'):
vowel_count+=1
file1.close()
pg. 11
2. Finding the lines starting with a particular character ‘A’.
f1 = open(‘LINES.txt’, ‘r’)
l = f1.readlines() # creates a list of all the lines in the file
print(‘Number of lines starting with A in the file LINES.txt is’, func(l))
2. f = open('testconn.txt')
m=f.readlines()
print(len(m))
pg. 12
1. Copy only the lines with character ‘”B” to another file.
2. A text file “sample1” which has multiple spaces between each
word.
The contents of “sample1” to be copied to another file “sample2” where the
multiple spaces must be reduced to single space.
1. Copy only the lines with character ‘”B” to another file.
f=open(“mm.txt”,”r”)
f1=open(“mm1.txt”,”w”)
X=f.readlines()
for m in X:
if m[0][0]==”B”:
f1.writeline(m)
f.close()
f1.close()
f1=open(“mm1.txt”,”r”)
d=f1.read()
print(d)
A text file “sample1” which has multiple spaces between each word.
f=open(“sample.txt”,”r”)
f1=open(“sample1.txt”,”w”)
x=f.readlines()
fori in x:
z=i.split()
for j in z:
z1=j+’ ‘
f1.write(z1)
f.close()
f1.close()
pg. 13
import pickle
defcreateFile():
ch="y"
fobj=open("EMP.dat","ab")
whilech=="y":
Empno=int(input("Empno : "))
Name=input("Name :")
Dept=input("Dept")
rec=[Empno,Name,Dept]
pickle.dump(rec,fobj)
x=input("Do you want to continue")
if x=="y":
continue
else:
break
fobj.close()
defCountRec(Dept):
fobj=open("EMP.dat","rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
ifDept==rec[2]:
num = num + 1
exceptEOFError:
fobj.close()
returnnum
#main
createFile()
City=input("Enter the Department")
X=CountRec(City)
print(X)
import pickle
defcreateFile():
ch=”y”
pg. 14
fobj=open("STU.dat","ab")
while ch=”y”:
EmpNo=int(input("Rollno : "))
Pro_name=input("Name :")
Dept=input(“Mark”)
rec=[Rollno,Name,Mark]
pickle.dump(rec,fobj)
x=input(“Do you want to continue”)
if x==”y”:
continue
else:
break
fobj.close()
defDispRec():
fobj=open("STU.dat","rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
if rec[2]>80
print(rec[0],rec[1],rec[2])
except:
fobj.close()
#main
Createfile()
Disprec()
'''Write a Python program to read a given CSV file having tab delimiter.
'''
import csv
with open('countries.csv', newline='') as csvfile:
data = csv.reader(csvfile, delimiter = '\t')
for row in data:
print(', '.join(row))
'''Write a Python program that reads each row of a given csv file
and skip the header of the file.
Also print the number of rows and the field names. '''
import csv
fields = []
rows = []
pg. 15
with open('departments4.csv', newline='') as csvfile:
data = csv.reader(csvfile, delimiter=' ', quotechar=',')
# Following command skips the first row of the CSV file.
fields = next(data)
for row in data:
print(', '.join(row))
print("\nTotal no. of rows: %d"%(data.line_num))
print('Field names are:')
print(', '.join(field for field in fields))
#Writerow
import csv
with open('employee_file.csv', mode='w') as employee_file:
employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"',
quoting=csv.QUOTE_MINIMAL)
employee_writer.writerow(['John Smith', 'Accounting', 'November'])
employee_writer.writerow(['Erica Meyers', 'IT', 'March'])
pg. 16
'''Write a Python program to write a Python dictionary
to a csv file. After writing the CSV file read the CSV
file and display the content. '''
import csv
csv_columns = ['id','Column1', 'Column2', 'Column3', 'Column4', 'Column5']
dict_data = {'id':['1', '2', '3'],
'Column1':[33, 25, 56],
'Column2':[35, 30, 30],
'Column3':[21, 40, 55],
'Column4':[71, 25, 55],
'Column5':[10, 10, 40], }
csv_file = "temp.csv"
try:
with open(csv_file, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()#writeheader() method writes the headers to the CSV
file.
for data in dict_data:
writer.writerow(dict_data)
exceptIOError:
print("I/O error")
data = csv.DictReader(open(csv_file))
print("CSV file as a dictionary:\n")
for row in data:
print(row)'''Write a Python program to write a Python list of lists to a csv
file. After writing the CSV file read the CSV
file and display the content.'''
import csv
data = [[10,'a1', 1], [12,'a2', 3], [14, 'a3', 5], [16, 'a4', 7], [18, 'a5', 9]]
with open("temp.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(data)
with open('temp.csv', newline='') as csvfile:
data = csv.reader(csvfile, delimiter=' ')
for row in data:
print(', '.join(row))
Create a CSV and do the following
1. Reading and writing
2. Reading the contents in the form of dictionary and display
3. Reading the contents in the form of list and print
1.READ
pg. 17
#import necessary modules
#csv_reader = csv.reader(csv_file, delimiter=',')
import csv
f=open("data.csv","r")
data = csv.reader(f)
for row in data:
print(row)
2.WRITE
#import necessary modules
import csv
file=open('Data2.csv','w')
writer = csv.writer(file, delimiter=',', quotechar='"',
quoting=csv.QUOTE_MINIMAL)
3.import csv
data = [[10,'a1', 1], [12,'a2', 3], [14, 'a3', 5], [16, 'a4', 7], [18, 'a5', 9]]
with open("temp.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(data)
with open('temp.csv', newline='') as csvfile:
data = csv.reader(csvfile, delimiter=' ')
for row in data:
print(', '.join(row))
4. '''Write a Python program to write a Python dictionary
to a csv file. After writing the CSV file read the CSV
file and display the content. '''
import csv
csv_columns = ['id','Column1', 'Column2', 'Column3', 'Column4', 'Column5']
dict_data = {'id':['1', '2', '3'],
'Column1':[33, 25, 56],
'Column2':[35, 30, 30],
'Column3':[21, 40, 55],
pg. 18
'Column4':[71, 25, 55],
'Column5':[10, 10, 40], }
csv_file = "temp.csv"
try:
with open(csv_file, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()#writeheader() method writes the headers to the
CSV file.
for data in dict_data:
writer.writerow(dict_data)
exceptIOError:
print("I/O error")
data = csv.DictReader(open(csv_file))
print("CSV file as a dictionary:\n")
for row in data:
print(row)
pg. 19