FILE HANDLING IN PYTHON
VAISHALI PHALNIKAR (KV SECTOR 8 R K PURAM )
Expected Learning outcomes :
1. What is file ? What are text file and binary file ?
2. Why do python program read from /write into file ?
3. Statements to open a file and statements to perform other
operations.
4. Write programs to do given task on files.(to do file handling)
Why do we need file operations
Till now we did many python programs . each time while executing we
gave the required input and got the output. Neither input nor output
was stored permanently . Why? Because both operations used
primary memory of system.
Applications like payroll, accounting system, inventory software
obviously can not be transient .They need some way to store data
permanently and allow programs to process the stored data. All these
programs are persistent
What is a file?
File is nothing but stream of bytes, nothing else.
PROGRAM IN PRIMARY
FILE IN HARD
MEMORY
DISK/SECONDARY MEMORY
File operations
Ø Creating a file
Ø Traversing a file for display data on screen
Ø Add data to existing a file
Ø Delete data from file
Ø Create a copy of a file
Ø Update data of file
Text and Binary Files
There are two types of files that 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. Unless mentioned all files are processed text format.
· 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 any files where the format isn't made up of readable characters.
Binary files can range from image files like JPEGs or GIFs, audio files like MP3s or
binary document formats like Word or PDF. In Python, files are opened in text mode
by default. To open files in binary mode, when specifying a mode, add 'b' to it.
There are two types of files that can be handled in python, normal text files and binary files
(written in binary language,that is 0s and 1s,computer understandable).
· 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. Unless mentioned all files are processed text format.
· 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 any files where the format isn't made up of readable characters. Binary
files can range from image files like JPEGs or GIFs, audio files like MP3s or binary
document formats like Word or PDF.
In Python, files are opened in text mode by default. To open files in binary mode, when
specifying a mode, add 'b' to it.
Difference between Text and Binary file
1. BINARY FILE:
TEXT FILE: 2. No such termination for line.
1. Each line of text file is terminated 3. Not so
by EOL char(‘\n’ in python)
2. Stored in human readable form 4. Faster processing
3. Slower processing 5. Developed specially for an application
4. Widely used file format nd can be and may not be understood by other
easily opened using a simple text applications
editor
5. Example any text file –a file created 6. Any .jpg or .png or PDF file
using notepad , OR any .rtf file 7. A single bit change also corrupts the
created using wordpad file
6. Less prone to get corrupted
8.
9.
How to open a file in Python:
When we want to read or write a file we have to open it. Open () does the work
Syntax: <file variable/file object or
handle>=open(file_name,access_mode)
Second argument of open() is optional and if we skip it then file is opened in read
mode.
Example :i) to open a file “notes.txt” in read mode
Python statement F=open(“notes.txt”,)
ii) to open a file “notes.txt” in write mode
Python statement F=open(“notes.txt”,’w’)
Concept of File modes
● “ r “, for reading.
● “ w “, for writing.
● “ a “, for appending.
● “ r+ “, for both reading and writing
● ‘rb’
One must keep in mind that the mode argument is not mandatory. If not passed, then
Python will assume it to be “ r ” by default.
File handling modes:
The second argument in open () function is used to specify file
mode.
We perform read, write, append operations on text and binary
files.
To open a file in binary mode a letter ‘b’ is to be used in file
mode.
By default file is opened in text mode.
We have to specify with ‘b’ to handle a file in binary mode
Next slide describes all modes
MODES DESCRIPTION
1. <r> It opens a file in read-only mode while the file offset/pointer stays at the root/begining.
2. <rb> It opens a file in (binary + read-only) modes. And the offset remains at the root level.
3. <r+> It opens the file in both (read + write) modes while the file offset is again at the root level.
4. <rb+> It opens the file in (read + write + binary) modes. The file offset is again at the root level.
5. <w> It allows write-level access to a file. If the file already exists, then it’ll get overwritten. It’ll create
a new file if the same doesn’t exist.
6. <wb> Use it to open a file for writing in binary format. Same behavior as for write-only mode.
7. <w+> It opens a file in both (read + write) modes. Same behavior as for write-only mode.
8. <wb+> It opens a file in (read + write + binary) modes. Same behavior as for write-only mode.
9. <a> It opens the file in append mode. The offset goes to the end of the file. If the file doesn’t exist,
then it gets created.
10. <ab> It opens a file in (append + binary) modes. Same behavior as for append mode.
11. <a+>It opens a file in (append + read) modes. Same behavior as for append mode.
12. <ab+> It opens a file in (append + read + binary) modes. Same behavior as for append mode.
Text File Handling
Expected learning outcome
1. To learn following functions used to read a file
read(),readline(),readlines()
1. To attempt questions based on text file handling asked in CBSE exam
Different ways to open a file for reading :
1.read():Syntax- <file handle>.read()
Reads at most n bytes.If n is not specified,reads entire file and returns
in the form of a string
2. readline(): Syntax-<file handle>.readline()
Reads a line of input, if n is specified reads n bytes of line and returns
in form of a string
3. readlines():Syntax-<file handle>.readlines()
Reads all lines and returns them in a list. Each line forms a element of a
list
read ( )
f_w=open("story.txt",'w') f_r=open("story.txt")
while True: #read entire file
s=input('Enter a line of text') str=f_r.read()
f_w.write(s) count=0
ans=input('Want to write more (y/n)?') print(str)
if ans !='y' : **write output of this program.
break Also use any integer value as argument in
read().
f_w.close()
readline( )
f_w=open("story.txt",'w') # open for line by line reading
no_of_lines = 5 f_r=open("story.txt")
lines = "" str=' '
print("Enter 5 lines") while str:
for i in range(no_of_lines): str=f_r.readline()
lines=input()+"\n" print(str,end=' ')
f_w.write(lines) ** try this program in IDLE. and write output
f_w.close()
readlines( )
# to create and write in a file "story.txt" f_r=open("story.txt")
f_w=open("story.txt",'w')
while True: #read entire file in a list
s=input('Enter a line of text') list=f_r.readlines()
f_w.write(s) print(list)
ans=input('Want to write more (y/n)?') ** write output for this program
if ans !='y' :
break
f_w.close()
Questions asked
Write a function calculate longest word in a file ‘story.txt’
Write a function to calculate length of a file in bytes
Def size_file(name) :
file=open(name,’r’)
str=file.read()
size=len(str)
print(‘size of a file in bytes=’,size)
Write a function to read a file ‘notes.txt’ and display only digits in a file
def digits_display():
f_r=open("notes.txt")
str=f_r.read()
count=0
for x in str:
if x.isdigit():
count+=1
print(x)
print('no of digits is ',count)
Write a function count no of ‘to’ in a file ‘story.txt’
def count_to():
f_r=open("story.txt")
str=f_r.read()
count=0
word=str.split(' ')
for x in word:
if x =='to':
count+=1
print('no of to s is ',count)
BINARY FILE HANDLINNG
What is a Binary File?
1. Binary files store data in binary form (computer understandable format)
2. Binary files are used to store data such as images, video, audio,
documents, database etc.
3. In a binary file, there is no delimiter to end a line. No internal conversion
like text file are done.
4. This is the reason binary file processing is faster
5. To process a binary file in python we use access mode ‘b’.
Expected Learning Outcome:
1. Insert/Append record in a binary file
2. Read records from a binary file
3. Search a record in a binary file
4. Update a record in a binary file
5. Delete a particular record
Simple most program to write a string in binary file
● # simple most program to write a string
● f_w=open('test.dat','wb')
● name=input('Enter name')
● name_en=name.encode()
● #encodes the string, using the specified encoding. If no encoding is #specified, UTF-8 will be used.
● f_w.write(name_en)
● f_w.close()
● f_r=open('test.dat','rb')
● name=f_r.read()
● name_de=name.decode()#
● print(name_de)
Binary File Handling using pickle module:
● When we write data structure like dictionary , tuple, array to a file ,we use module
Pickle of python.
● Picking refers to process of converting data structure into byte stream before
writing to file.
● We will use 2 methods of pickle module
● dump(object,file object) is used to convert object to byte stream and write into a
file
● object=load(file object)is used to read from file to object
● When we write object into a file we use dump() and when we read from file into an
object we use load().
Steps for binary file processing
To write data to a file To read data from a file
1. Create a file using open () in binary 1. Open a file in ‘rb’ mode
access mode (‘wb’)
2. Take input from user (usually in
2. Use load () method to
list,tuple,dictionary data types) de serialize object from file
3. Use dump() of pickle module object
to serialize the Syntax:
object(list/tuple/dictionary into byte
stream) object=pickle.load(file object)
● Syntax: pickle.dump(object,file 3. Process it
object)
A tuple is written in a binary file A dictionary is written in a binary file
● t=((1,"anita",89),(2,"basant",67), ● phone={"ankit":"5865698","nimi
(3,"kareena",56),(4,"tushar",96)) sh":"46445545","jitendra":"4343
● f=open("tuple.dat",'wb') 4343"}
● pickle.dump(t,f) ● f=open("dict.dat","ab")
● print(‘Tuple written to file’) ● pickle.dump(phone,f)
● f.close() ● print("dictionary written")
● f.close()
● f=open("tuple.dat","rb") ● f=open("dict.dat",'rb')
● tt=pickle.load(f) ● p=pickle.load(f)
● print(tt) ● print(p)
To add students details like roll, name ,percentage
using dictionary into a binary file
● import pickle
● def student_records(filename):
● f_w=open(filename,'wb')
● while True:
● rn=input('enter roll number of a student')
● na=input('enter name of a student')
● perc=float(input('Enter percentage '))
● d={'roll':rn,'name':na,'percentage':perc}
● pickle.dump(d,f_w)
● ans=input('want to add more records y/n ?')
● if ans!='y':
● break
● f_w.close()
● def read_records(filename):
● f_r=open(filename,'rb')
● while True:
● try:
● dict=pickle.load(f_r)
Search a record from ‘student.dat’
Search on base of roll no
● def search(roll_no):
● f_r=open('student.dat','rb')
● flag=False
● while True:
● try:
● rec=pickle.load(f_r)
● if rec['roll']==roll_no:
● flag=True
● for key in rec:
● print(key,rec[key])
● except EOFError:
● break
● if flag==False:
● print('no such record in file')
● f_r.close()
To modify marks of student for given roll no
● def modify_marks(r,m):
● f_r=open('student.dat','rb')
● rec_lst=[]
● while True:
● try:
● rec=pickle.load(f_r)
● rec_lst.append(rec)
● except EOFError:
● break
● f_r.close()
● for i in range(len(rec_lst)):
● if rec_lst[i]['roll']==r:
● rec_lst[i]['percentage']=m
● f_w=open('student.dat','wb')
● for x in rec_lst:
● pickle.dump(x,f_w)
● f_w.close()
To delete a record of a given roll no
● def delet_record(r):
● f_r=open('student.dat','rb')
● rec_lst=[]
● while True:
● try:
● rec=pickle.load(f_r)
● rec_lst.append(rec)
● except EOFError:
● break
● f_r.close()
● f_w=open('student.dat','wb')
● for x in rec_lst:
● if x['roll']==r:
● continue
● pickle.dump(x,f_w)
● f_w.close()
CSV FILE HANDLING IN PYTHON
WHAT IS A CSV FILE?
● A CSV file (Comma Separated Values file) is a type of plain text file that uses
specific structuring to arrange tabular data. Because it’s a plain text file, it
can contain only actual text data—in other words,
printable ASCII or Unicode characters.
●
HOW IT LOOKS?
● The structure of a CSV file is given away by its name.
● Normally, CSV files use a comma to separate each specific data value.
Here’s what that structure looks like:
name,class,hobby
abhinav,9 a,cricket
tuhin,5b,music
ziya,7c,badminton
sumedh,9a,chess
MORE ABOUT ITS STRUCTURE..
● Notice how each piece of data is separated by a comma. Normally, the first line
identifies each piece of data—in other words, the name of a data column. Every
subsequent line after that is actual data and is limited only by file size constraints.
● In general, the separator character is called a delimiter, and the comma is not the
only one used.
● Other popular delimiters include the tab (\t), colon (:) and semi-colon (;) characters.
● Properly parsing a CSV file requires us to know which delimiter is being used.
WHERE DO CSV FILES COME FROM?
● CSV files are normally created by programs that handle large amounts of data.
● They are a convenient way to export data from spreadsheets and databases as
well as import or use it in other programs.
● For example, you might export the results of a data mining program to a CSV file
and then import that into a spreadsheet to analyze the data, generate graphs for a
presentation, or prepare a report for publication.
● CSV files are very easy to work with programmatically. Any language that supports
text file input and string manipulation (like Python) can work with CSV files directly.
PYTHON WAY TO DEAL WITH CSV FILE
● Reading a csv file:
● Import module csv
● Open a file with open () function
● Declare a object for reader () function of csv
● Syntax :reader object=csv.reader(file handle/object)
● Use for loop on reader object to read records in a file.
READING CSV FILES WITH CSV READER
● Reading from a CSV file is done using the reader object.
● The CSV file is opened as a text file with Python’s built-in open() function,
which returns a file object.
● This is then passed to the reader, which does the heavy lifting.(content
/records of csv file)
READ A CSV FILE AS EACH RECORD AS A LIST
● import csv
● f=open('E:/vaishali_school_work_2020/cs_work/studenthobbydata1.csv','r')
● csv_reader=csv.reader(f)
● for row in csv_reader:
● print(row)
●
● f.close()
READ OPERATION ON CSV FILE AND
DISPLAY AS A LIST(RECORDS ARE
APPENDED IN SINGLE LIST)
● with open('E:/vaishali_school_work_2020/cs_work/studenthobbydata1.csv','r') as
csv_file:
● reader=csv.reader(csv_file)
● rows =[]
● for rec in reader:
● rows.append(rec)
● print(rows)
● Difference between with open () and open () is with open() close() is automatically
called
TO COUNT NO OF RECORDS IN A CSV FILE
● with open('E:/vaishali_school_work_2020/cs_work/studenthobbydata1.csv','r') as
csv_file:
● reader=csv.reader(csv_file)
● col=next(reader) # as we want to count records
● # we are skipping names of fields (first row)
● c=0
● for row in csv_file:
● c=c+1
● print('no of records are',c)
TO DISPLAY CONTENT OF CSV FILE SEPERATED BY
COMMA
● with
open('E:/vaishali_school_work_2020/cs_work/studenthobbydata1.csv','r') as
csv_file:
● reader=csv.reader(csv_file)
●
● for rec in reader:
● print(','.join(rec))
SEARCH BY NAME IN CSV FILE
● with open('E:/vaishali_school_work_2020/cs_work/studenthobbydata1.csv','r') as
csv_file:
● reader=csv.reader(csv_file)
● name=input('enter a name whose record is to be searched')
●
● for rec in reader:
● if rec[0]==name:
● print(rec)
WRITING CSV FILES WITH CSV WRITER
Writing a CSV file is done using the writer object.
● The CSV file is opened as a text file with Python’s built-in open() function,
which returns a file object. Use ‘w’ file access mode
● The csv.writer() function is used to create a writer object.
● The writer.writerow() function is then used to write single row to the CSV file.
Use for loop to write more than one row.
● Writer.writerows() also can be used multiple rows in a file.
PROGRAM TO WRITE A CSV FILE
● import csv
● fields=['name','class','year','percent']
● row=[['anamika','12A','2019','92'],
● ['ASEEM','12B','2020','90'],
● ['nina','12c','2019','89'],
● ['vineeta','12d','2017','93']
● ]
● filename='E:/vaishali_school_work_2020/cs_work/studentdetails.csv'
● with open(filename,'w',newline='') as csv_file:
● writer=csv.writer(csv_file,delimiter=',')
● writer.writerow(fields)
● for i in row:
● writer.writerow(i)
● print('CSV FILE written')