File Handling Notes
File Handling Notes
ABOUT FILE:
A file is a sequence of bytes contain data and store it on some storage device. File handling is an
important part of any computer application.
➔ Sometime the output generated by a program is large thus file help to store that huge data.
TYPES OF FILES
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. In text file, translation will take place after the EOL or delimiter
is encountered. Thus, it is slower than binary file.
Binary Files:
A binary file stores the data in the same way as stored in the memory. In Binary file there is no
delimiter for a line. The file contents returned by a binary file is raw i.e. with no translation, thus Binary
files are faster than text files.
CSV Files:
CSV stands for Comma Separated Values. CSV is just like a text file, in a human readable format
which is extensively used to store tabular data, in a spreadsheet or database. The separator character of
CSV files is called a delimiter. Default delimiter is comma (,). Other delimiters are tab (\t), colon (:), pipe (|),
semicolon (;) characters.
In Python, any type of File Handling consists the following three steps:
1. Opening of a file with specific mode. Using open () method / with statement of python.
The first step of file handling is to open the file by specifying file-path and file-mode. If no file-mode is
specified by default file will be open in reading mode.
Example: f = open(“demo.txt”,”r”)
2) Using Python ‘with’ statement: -
Absolute Path:
• Absolute file paths are notated by a leading forward slash or drive label.
Example C:\\KVSROMUMBAI\\demo.txt
• An absolute path, which always begins with the root folder.
Relative Path:
A relative path, is relative to the program’s current working directory.
CLOSING A FILE: -
close() method is use to close the opened file. In python files are automatically close at the end of the
program but it is advisable to close a file after the end of the program.
S yntax: <fileObject>.close()
Example : f.close()
Moreover, if file has opened using ‘with’ statement of python then it will automatically close after
the nested block of code.
1. read() - The read() method returns the whole text in the form of string. You can also specify how
many characters you want to return by passing the size as argument.
Syntax: <file_object>.read([n]) where n is the No. of bytes that we want to read from the file.
b) <file_object>.read(n) – It will read exactly n bytes from the beginning of the file.
Example:
f = open("demo.txt", "r")
Syntax: <file_object>.readline()
Example
f = open("demo.txt", "r")
3. readlines() – This method will return a list of strings, each separated by \n. readlines() can be used to read
the entire content of the file.
Syntax: <file_object>.readlines()
# Program to print the first and last line of the text file abcd.txt
myf=open("abcd.txt","r")
lst=myf.readlines()
print(lst[0]) # Used to print the first line
print(lst[-1]) # Used to print the last line
myf.close()
myf=open("abcd.txt","r")
lst=myf.readlines()
n =int(input('Enter how many lines you wish to print from last'))
for i in range(-1,-n-1,-1):
print(lst[i])
myf.close()
myf=open("myfile.txt","r")
x=myf.read()
c=0
for i in x:
if(i.isdigit()):
c=c+1
print('Total digits=',c)
myf.close()
The read() method reads data from a text file, and store its contents in a string type variable. It
can either read the whole content from the file if any parameter is not passed inside read method,
otherwise if n is passed inside read() the method will read n number of characters.
The readline() function reads data line by line from the text file. At a time it will return one line
of data in the form of string.
The readlines() function reads all lines and return them in a list.
Now, carefully watch the given code and then identify the data type of record1 and record2?
file = open("portfolio.txt")
record1 = file.readlines()
file.close()
file = open("portfolio.txt")
record2 = file.read()
file.close()
FILE = open("quotes.txt")
LC = 0
DATA = FILE.readlines()
for L in DATA:
if L[0] == 'B':
LC += 1
print("No. of lines starts with B =",LC)
FILE.close()
Output:
FILE = open("quotes.txt")
AC = 0
DATA = FILE.read()
for L in DATA:
if L=='B':
AC+=1
print("Total No. of alphabet B in the file =",AC)
FILE.close()
Output:
A Program can also write strings into a text file. Followings are the methods to write a data to the
file:
write ()
writelines()
Note: - To write into a text file, we must add parameter as ‘w’ or ‘a’ in the open() function which specifies the
"w" – for overwrite any existing content and then write the new data
Difference between ‘a’ and ‘w’ modes i.e. append and write modes: -
If you open a file in “w” means write mode, Python overwrites an existing file or creates a new
file. For an existing file, the earlier data gets removed.
If you want to write into a file with retaining the previous data, the file must be opened in “a”
means append mode.
1) write() – This method takes a string ( as parameter ) and writes it in the file. For storing data with end of line
character, we will have to add \n character to the end of the string.
Example:
f = open("demo.txt", "w")
f.close()
print(f.read())
# Open the previous file "demo.txt" and add more content to the file:
f = open("demo.txt", "a")
f.close()
# Now again open the file in read mode and see the result …. (Check by yourself)
2) writelines() - Drawback of write() function is it will write a string in a text file at a time, and it can't be
used for writing a list, tuple etc. into a file. writelines() method will help to write a sequence of strings to the
file. The sequence can be any object producing strings, typically a list of strings. So, whenever we have to
write a sequence of string / data type, we will use writelines(), instead of write().
f = open("demo.txt", "w")
f.writelines(["Python is just amazing!", "\nWe love python"]) # here we used list data
f.close()
f = open("demo.txt", "r")
print(f.read())
flush() function: - The flush() function forces the writing of data on disc which was still pending in the
output buffer.
Syntax : <file_object>.flush()
f = open("demo_flush.txt","w+")
f.write("India is my country.\n")
f.flush()
x=”Jai Hind”
f.write(x)
f.seek(0) # Here seek() will move the file pointer(handle) at the beginning of the file.
print(f.read())
f.close()
seek() function: – The seek() function changes the position of the file-pointer by placing the file-pointer at
the specified position in the open file.
Offset will be 0 or 1 or 2
0 for beginning of the file (to move file pointer w.r.t. beginning of file) it is default mode.
1 for current position of file pointer (to move file pointer w.r.t current position of it.)
2 for end of file (to move file-pointer w.e.t. end of file)
Example:
f=open("demo_seek.txt","r")
print(f.read()) # after reading all the bytes file pointer reaches to the end of the file.
f.seek(6) # Now the file pointer will move to the 6th Byte from beginning.
print(f.read()) # It will print the remaining number of bytes after 6th Byte.
tell() function: – This function returns the current position of file pointer in the file.
Syntax: <file_object>.tell()
Example:
f=open("d:\\pooja\\demo_seek.txt","r")
print(f.read(5)) # This will read 5 bytes from the beginning
print(f.tell()) # This will show 5 as file pointer is at 5th character from beginning
BINARY FILES:
A Binary file stores the information in the form of a stream of bytes. A binary file stores the data
in the same way as stored in the memory. In Binary file there is no delimiter for a line. The file contents
returned by a binary file is raw i.e. with no translation, thus Binary files are faster than text files.
Python objects (list, dictionary etc) have a specific structure which must be maintained
while storing or accessing them. Python provides a special module called pickle module for this.
PICKLING refers to the process of converting the structure(list/dictionary) to a byte of stream before
writing it to a file. The process to converts any kind of python objects (list, dict etc.) into byte streams (0s
and 1s).
UNPICKLING is used to convert the byte stream back to the original structure while reading the
contents of the file.
pickle Module: -
Before reading or writing to a file, we have to import the pickle module.
import pickle
pickle.dump() – This method is used to write the object in the file which is opened in ‘wb’ or ‘ab’
i.e. write binary or append binary access mode respectively.
Syntax : pickle.dump(<structure>,<FileObject>)
import pickle
fo = open("binary_file1.dat","wb")
Laptop = ["Dell","HP","ACER"]
pickle.dump(Laptop,fo)
fo.close()
pickle.load() – This method is used to read data from a file and return back into the structure (list/dictionary).
Structure can be any sequence in Python such as list, dictionary etc. FileObject is the file handle of file in
which we have to write.
fbin = open("binary_file1.dat","rb")
x=pickle.load(fbin)
print(x)
fbin.close()
# Simple program to write a dictionary data into a binary file
import pickle
f=open("my_bin1.bin","wb")
D1={3:'Maruti',2:'Honda',4:'Hundai',1:'BMW'}
pickle.dump(D1,f)
f.close()
f1=open("my_bin1.bin","rb")
D2=pickle.load(f1)
print(D2)
f.close()
# Write a User defined function bdict() to store customer data into a binary file customer.dat using a
dictionary and print them on screen after reading them. The customer data contains
customer_ID(c1,c2,c3) as key, and name, city as values.
import pickle
def bdict():
f = open("customer.dat","wb")
d = {'C1':['Siman Raheja','Haryana'],
'C2':['Praharsh Kumar','Pune'],
'C3':['Vinita Minj','Indore']}
pickle.dump(d,f)
f.close()
f = open("customer.dat","rb")
d = pickle.load(f)
print(d)
f.close()
# Sample program to insert any number of records (as per user’s choice) of employee (employee number,
name, salary and allowance) and then display all the records.
import pickle
bfile=open("empfile.dat","ab")
recno=1
while True:
print("RECORD No.", recno)
eno=int(input("\tEmployee number : "))
ename=input("\tEmployee Name : ")
ebasic=int(input("\tBasic Salary : "))
allow=int(input("\tAllowances : "))
totsal=ebasic+allow
print("\tTOTAL SALARY : ", totsal)
edata=[eno,ename,ebasic,allow,totsal]
pickle.dump(edata,bfile)
# Write a program that have a binary file “Book.dat”. The file has structure [BookNo, Book_Name, Author,
Price]. Now do as directed: -
1) Write a user defined function CreateFile() to input data for a record and add to Book.dat.
2) Write the definition of show_all_books() UDF to display the details of all the books.
3) Write a User Defined function CountRec(Author) in Python which accepts the Author name as
parameter, display the details of the book of the same author also return the number of books of the
given Author are stored in the binary file “Book.dat"
import pickle
def createfile():
fobj=open("Book.dat","ab")
def show_all_books():
fobj=open("Book.dat","rb")
L1=[]
try:
while True:
L1=pickle.load(fobj)
print(L1)
except EOFError:
print('All Record displayed')
pass
fobj.close()
def countrec(Author):
fobj=open("Book.dat", "rb")
cnt = 0
print("Book No Book Name Author Price")
try:
while True:
r=pickle.load(fobj)
if Author==r[2]:
cnt = cnt + 1
print(r[0],"\t",r[1],"\t",r[2],'\t',r[3])
except:
print()
fobj.close()
return cnt
# Write a program that uses a binary file “STUDENT.DAT” has structure [roll_number, Name, Percentage].
Write a User defined function countrec() that would read contents of the file
“STUDENT.DAT” and display the details of those students whose percentage is above 75%.
import pickle
def createfile():
fobj=open("student.dat","ab")
rec=[rno, nm , p]
pickle.dump(rec, fobj)
print("Record saved")
fobj.close()
def countrec():
fobj=open("student.dat","rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
if rec[2]>375:
num = num + 1
print(rec[0],rec[1],rec[2])
except:
fobj.close()
return num
createfile()
countrec()
CSV FILES: -
CSV stands for Comma Separated Values. It is a type of plain text file that uses specific structure to
arrange tabular data i.e. data stored in rows and columns such as a spreadsheet or database.
CSV is like a text file, It is in human readable format and extensively used to store tabular data, in a
spreadsheet or database.
Each line of the csv file is a data record. Each record consists of one or more fields, separated by
commas. The separator character of CSV files is called a delimiter.
Default delimiter is (,). Other delimiters are tab(\t), colon (:), pipe(|), semicolon (;) characters.
To read data from csv files, reader() method of csv module is used.
csv.reader() returns a reader object.
STEPS TO READ
import csv
f = open("emp.csv","r")
emp_reader=csv.reader(f)
for row in emp_reader:
print(row[0],row[1],row[2]) # print(row) : As a list
f.close()
To write data into csv files, writer() function of csv module is used.
csv.writer(): This function returns a writer object which writes data into writer object.
Significance of writer object
The csv.writer() returns a writer object that converts the data into a delimited string.The string
can be later converted into csv files using the writerow() or writerows() method.
Syntax:
<writer_object>.writerow() :
<writer_object>.writerows()
# Program to write data into a CSV File and to read data stored in csv file
import csv
f=open("mycsv.csv","w",newline='')
w=csv.writer(f)
lst=["RNO","NAME","MARKS"]
w.writerow(lst)
f=open("mycsv.csv","r")
rec=csv.reader(f)
for i in rec:
print(i)
f.close()
# Program to write Employee Name, EmpID and Dept for some employees in a csv file then display
records of all the employees.
import csv
f=open("emp.csv","w",newline='')
emp_writer = csv.writer(f)
emp_writer.writerow(["EmpName","EmpID","Dept"])
emp_rec = []
while True:
print("Enter Employee details: ")
empname = input("EmpName : ")
eid = int(input("EmpID : "))
dept = input("Department : ")
emp_rec.append([empname,eid,dept])
if ch == "N" or ch =="n":
break
emp_writer.writerows(emp_rec)
f.close()
f=open("emp.csv","r")
rec=csv.reader(f)
for i in rec:
print(i)
f.close()
Madhwan, is doing internship in “SQUARE Solutions Pvt. Ltd.”. He wrote the following python code to store
student’s data in csv file (Student.csv) handling. Unfortunately, he forgot some steps of the python code.
Please help him to create a CSV File 'Student.csv' by completing the code.
CSV File
1,SAKSHAM,XII,A
2,ARNAV,XI,A
3,SHREEVALI,XII,A
4,BHOOMI,XI,A
5,SWARIT,XII,A
import_____ #Statement-1
data = []
data.append(header)
for i in range(5):
roll_no = int(input("Enter Roll Number : "))
name = input("Enter Name : ")
Class = input("Enter Class : ")
section = input("Enter Section : ")
rec = [_____] #Statement-4
data.append(rec)
stuwriter. _____ (data) #Statement-5
csvfh.close()
a) csv file
b) CSV
c) csv
d) Csv
a) "School.csv","w"
b) "Student.csv","w"
c) "Student.csv","r"
d) "School.csv","r"
a) reader(csvfh)
b) reader(MyFile)
c) writer(csvfh)
d) writer(MyFile)
e) Choose the function name that should be used for Statement-5 to create the desired CSV File.
a) dump()
b) load()
c) writerows()
d) writerow()