1. FILE HANDLING
INTRODUCTION
DATA FILES
OPENINGANDCLOSING FILES
READINGANDWRITING FILES
STANDARD INPUT,OUTPUTAND ERRORSTREAMS
2. Introduction
FILE HANDLING is a mechanism by which we
can read data of disk files in python program or
write back data from python program to disk
files.
So far in our python program the standard input
in coming from keyboard an output is going to
monitor i.e. no where data is stored permanent
and entered data is present as long as program is
running BUT file handling allows us to store data
entered through python program permanently in
disk file and later on we can read back the data
3. DATA FILES
It contains data pertaining to a specific
application, for later use. The data files can be
stored in two ways –
Text File
Binary File
4. Text File
Text file stores information in ASCII OR
UNICODE character. In text file everything will
be stored as a character for example if data is
“computer” then it will take 8 bytes and if the
data is floating value like 11237.9876 it will take
10 bytes.
In text file each line is terminated by special
character called EOL(End of line). In text file
some translation takes place when this EOL
character is read or written. In python EOL is ‘n’
or ‘r’ or combination of both
5. Binary files
Binary files are stored in terms of bytes (0s
and 1s)
It stores the information in the same format
as in the memory i.e. data is stored according
to its data type so no translation occurs.
In binary file there is no delimiter for a new
line
Binary files are faster and easier for a
program to read and write than text files.
Data in binary files cannot be directly read, it
can be read only through python program for
the same.
6. File Pointer
Every file maintains a file pointer which tells the
current position in the file where reading and
writing operation will take.
When we perform any read/write operation two
things happens:
The operation at the current position of file pointer
File pointer advances by the specified number of
bytes.
7. Example
myfile = open(“ipl.txt”,”r”)
File pointer will be by default at first position i.e. first character
ch = myfile.read(1)
ch will store first character i.e. first character is consumed, and file pointer will
move to next character
8. Steps in Data File Handling
1. OPENING FILE
We should first open the file for read or write by
specifying the name of file and mode.
2. PERFORMING READ/WRITE
Once the file is opened now we can either read or
write for which file is opened using various functions
available
3. CLOSING FILE
After performing operation we must close the file
and release the file for other application to use it,
9. Opening File
File can be opened for either – read, write,
append.
SYNTAX:
file_object = open(filename)
Or
file_object = open(filename,mode)
** default mode is “read”
10. Opening File
myfile = open(“story.txt”)
here disk file “story.txt” is loaded in
memory and its reference is linked to “myfile”
object, now python program will access
“story.txt” through “myfile” object.
here “story.txt” is present in the same
folder where .py file is stored otherwise if disk
file to work is in another folder we have to give
full path.
11. Opening File
myfile = open(“article.txt”,”r”)
here “r” is for read (although it is by default, other
options are “w” for write, “a” for append)
myfile = open(“d:mydatapoem.txt”,”r”)
here we are accessing “poem.txt” file stored in
separate location i.e. d:mydata folder.
at the time of giving path of file we must use double
backslash() in place of single backslash because in python
single slash is used for escape character and it may cause
problem like if the folder name is “nitin” and we provide path
as d:nitinpoem.txt then in nitin “n” will become escape
character for new line, SO ALWAYS USE DOUBLE
BACKSLASH IN PATH
12. Opening a file using with clause
In Python, we can also open a file using with
clause. The syntax of with clause is:
with open (file_name, access_mode) as file_
object:
The advantage of using with clause is that any
file that is opened using this clause is closed
automatically,
Example:
with open(“myfile.txt”,”r+”) as myObject:
content = myObject.read()
13. File Access Mode
Text File
Mode
Binary File
Mode
Description Notes
‘r’ ‘rb’ Read only File must exists, otherwise Python raises
I/O errors
‘w’ ‘wb’ Write only If file not exists, file is created
If file exists, python will truncate
existing data and overwrite the file.
‘a’ ‘ab’ Append File is in write mode only, new data will
be added to the end of existing data i.e.
no overwriting. If file not exists it is
created
‘r+’ ‘r+b’ or ‘rb+’ Read and write File must exists otherwise error is raised
Both reading and writing can take place
w+ ‘w+b’ or ‘wb+’ Write and read File is created if not exists, if exists data
will be truncated, both read and write
allowed
‘a+’
VINOD K
SAC
‘a+b’ or ‘ab+’
UMARVERMA,PGT(CS),
HIN BHARDWAJ, PGT(CS)
Write and read
KV OEF KANPUR&
, KV NO.1TEZPUR
Same as above but previous content will
be retained and both read and write.
14. Closing file
As reference of disk file is stored in file handle
so to close we must call the close() function
through the file handle and release the file.
myfile.close()
Note: open function is built-in function used
standalone while close() must be called through file
handle
15. Reading from File
To read from file python provide many functions
like :
Filehandle.read([n]) : reads and return n bytes,
if n is not specified it reads entire file.
Filehandle.readline([n]) : reads a line of input.
If n is specified reads at most n bytes. Read bytes
in the form of string ending with line character or
blank string if no more bytes are left for reading.
Filehandle.readlines(): reads all lines and
returns them in a list
20. Example 5 To display data from a text
file
fin=open("practice.txt","r")
str = fin.readline()
while str:
print(str)
str=fin.readline() fin.close()
OUTPUT IS:
i am in mumbai
i like music
i love india
22. Questions…
1) Write python code to display each word of a line separately as an
element of a list.
Answer:
myobject=open("myfile.txt",'r')
d=myobject.readlines()
for line in d:
words=line.split()
print(words)
Output is:
23. In the output, each string is
returned as elements of a list.
However, if splitlines() is used
instead of split(), then each line
is returned as element of a list,
as shown in the output below:
>>> for line in d:
words=line.splitlines()
print(words)
Output is:
[‘I am an indian’]
[‘My country is india’]
24. fobject=open("testfile.txt","w")
sentence=input("Enter the contents to
be written in the file: ")
fobject.write(sentence)
fobject.close()
print("Now reading the contents of
the file: ")
fobject=open("testfile.txt","r")
for str in fobject:
print(str)
fobject.close()
Writing and reading to a text file
26. write() - for writing a single string
writeline() - for writing a sequence
of strings
The write() method
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.
27. writelines() method
This method is used to write multiple
strings to a file.
We need to pass an iterable object like lists,
tuple, etc. containing strings to the
writelines() method.
Unlike, write(), the writelines() method
does not return the
number of characters written in the file.
28. Setting Offsets in a File
The functions that we have learnt till now are used to
access the data sequentially from a file. But if we want
to access data in a random fashion, then Python gives us
seek() and tell() functions to do so.
The tell() method
This function returns an integer that specifies the
current position of the file object in the file. The position
so specified is the byte position from the beginning of
the file till the current position of the file object. The
syntax of using tell() is:
file_object.tell()
29. The seek() method
This method is used to position the file object at a particular
position in a file.
The syntax of seek() is:
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.
30. Seek() function:
For example,
1. f.seek(–10,1) from current position,
move 10 bytes backward
2. f.seek(10,1) from current position,
move 10 bytes forward
3. f.seek(–20,1) from current position,
move 20 bytes backward
4. f.seek(10,0) from beginning of file,
move 10 bytes forward
31. Application of seek() and tell()
print("Learning to move the file object")
fileobject=open("testfile.txt","r+")
str=fileobject.read()
print(str)
print("Initially, the position of the file object is: ",fileobject.
tell())
fileobject.seek(0)
print("Now the file object is at the beginning of the
file:",fileobject.tell())
fileobject.seek(5)
print("The position of the file object is at", fileobject.tell())
str=fileobject.read()
print(str)
32. Output is:
Learning to move the file object
i am good
Initially, the position of the file
object is: 9
Now the file object is at the
beginning of the file: 0
The position of the file object is at
5
good
33. To create a text file and write
multiple lines in it
fileobject=open("practice.txt","w+")
while True:
data= input("Enter data to save in the text file: ")
fileobject.write(data)
ans=input("Do you wish to enter more data?(y/n): ")
if ans=='n': break
fileobject.close()
34. Output is:
Enter data to save in the text
file: I am interested to learn
about Computer Science
Do you wish to enter more
data?(y/n): y
Enter data to save in the text
file: Python is easy to learn
Do you wish to enter more
data?(y/n): n
35. To read and display data that is
stored in a text file :
fileobject=open("practice.txt","r")
str = fileobject.readline()
while str:
print(str)
str=fileobject.readline()
fileobject.close()
the readline() is used in the while
loop to read the data line by line
from the text file. The lines are
displayed using the print().
36. To perform reading and writing
operation in a text file using a+ mode
fileobject=open("report.txt", “a+")
print ("WRITING DATA IN THE FILE")
print() # to display a blank line
while True:
line= input("Enter a sentence ")
fileobject.write(line)
fileobject.write('n')
choice=input("Do you wish to enter more data? (y/n): ")
if choice in ('n','N'): break
print("The byte position of file object is
",fileobject.tell())
fileobject.seek(0) #places file object at beginning of file
print()
print("READING DATA FROM THE FILE")
str=fileobject.read()
print(str)
37. Output is:
WRITING DATA IN THE FILE
Enter a sentence=hello how are you?
Do you wish to enter more data?
(y/n): n
The byte position of file object is
34
READING DATA FROM THE FILE=
i love india
hello how are you?
38. Binary File Operation using
Pickle Module
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.
Serialization is the process of
transforming data or an object in memory
(RAM) to a stream of bytes called byte
streams. These byte streams in a binary
file can then be stored in a disk or in
a database or sent through a network.
Serialization process is also called
pickling.
39. De-serialization or unpickling is the inverse of
pickling process where a byte stream is converted
back to Python object.
The pickle module deals with binary files. Here,
data are not written but dumped and similarly,
data are not read but loaded. The Pickle Module
must be imported to load and dump data. The
pickle module provides two methods - dump()
and load() to work with binary files for pickling
and unpickling, respectively.
40. Steps to perform binary file operations
First we need to import the module called
pickle.
This module provides 2 main functions:
dump() : to write the object in file which is loaded
in binary mode
Syntax : dump(object_to_write, filehandle)
load() : dumped data can be read from file using
load() i.e. it is used to read object from pickle file.
Syntax: object = load(filehandle)
41. dump () method
This method is used to convert (pickling) Python
objects for writing data in a binary file. The file in
which data are to be dumped, needs to be opened
in binary write mode (wb).
Syntax of dump() is as follows:
dump(data_object, file_object)
where data_object is the object that has to be
dumped to the file with the file handle named file
object.
42. Ex= write the record of a student
(roll_no, name, gender and marks)
in the binary file named
mybinary.dat using the dump(). We
need to close the file after
pickling
import pickle
listvalues=[1,"Geetika",'F', 26]
fileobject=open("mybinary.dat", "wb")
pickle.dump(listvalues,fileobject)
fileobject.close()
43. Example: dump()
See the content is some kind
of encrypted format, and it is
not in complete readable
form
46. Program to search a record from the
binary file "student.dat" on the
basis of roll number
47. Updating a record in the file requires roll
number to be fetched from the user whose name
& marks are to be updated.
import pickle
f=open('student.dat','rb+')
found=0
r=int(input('Enter roll no to search :'))
while True:
rec=pickle.load(f)
if rec['Rollno']==r:
print('Current name is:',rec['Name'])
print('Current Marks are:',rec['Marks'])
rec['Name']=input('Enter new name:')
rec['Marks']=int(input('Enter new marks:'))
found=1
break
if found==1:
f.seek(0)
pickle.dump(rec,f)
print('Record updated')
f.close()
48. The load() method
This method is used to load (unpickling) data from
a binary file. The file to be loaded is opened in
binary read(rb) mode.
Syntax of load() is as follows:
Store_object = load(file_object)
Here, the pickled Python object is loaded from the
file having a file handle named file_object and is
stored in a new file handle called store_object.
50. Example
Unpickling data in Python
import pickle
print("The data that were stored in file are: ")
fileobject=open("mybinary.dat","rb")
objectvar=pickle.load(fileobject)
fileobject.close()
print(objectvar)
Output of Program 2-7:
>>>
RESTART: Path_to_fileProgram2-7.py
The data that were stored in file are:
[1, 'Geetika', 'F', 26]
53. Removing whitespaces after reading
from file
read() and readline() reads data from file and
return it in the form of string and readlines()
returns data in the form of list.
All these read function also read leading and
trailing whitespaces, new line characters. If you
want to remove these characters you can use
functions
strip() : removes the given character from both ends.
lstrip(): removes given character from left end
rstrip(): removes given character from right end
55. File Handling:
CSV FILE (Comma separated value)
CSV (Comma Separated Values) is a file format for
data storage which looks like a text file. The
information is organized with one record on each
line and each field is separated by comma.
CSV File Characteristics:
• One line for each record
• Comma separated fields
• Space-characters adjacent to commas are
ignored
• Fields with in-built commas are separated by
double quote characters.
56. When to Use CSV?
• When data has a strict tabular
structure
• To transfer large database between
programs
• To import and export data to office
applications, Qedoc modules
• To store, manage and modify
shopping cart catalogue
57. CSV Advantages
• CSV is faster to handle
• CSV is smaller in size
• CSV is easy to generate
• CSV is human readable and easy to edit
manually
• CSV is simple to implement and parse
• CSV is processed by almost all existing
applications
58. CSV Disadvantages
• No standard way to represent binary
data
• There is no distinction between text and
numeric values
• Poor support of special characters and
control characters
• CSV allows to move most basic data only.
Complex configurations cannot be
imported and exported this way
• Problems with importing CSV into SQL
(no distinction between NULL and quotes)
59. Write / Read CSV FILE
Writing and reading operation from text file is
very easy.
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 which create newFile like
object.
Through csv.writer() method ,we create writer
object to call writerow() method to write objects.
60. Reading data from CSV file
Similarly for reading ,
we open the file in ‘r’ mode and create new
File like object, further we create new file
reader object using csv.reader()method to
read each row of the file.
Three file opening modes are there ‘w’,’r’,’a’.
‘a’ for append .
After file operation close opened file using
close() method.