3-file-handling
3-file-handling
Program in RAM
(Random Har
User
Access d
Memory) Dis
k
Why the Files are used?
• The data stored with in a file is known as persistent data because
this data is permanently stored in the system.
• Python provides reading and writing capability of data files.
• We save the data in the files for further use.
• As you save your data in files using word, excel etc. same thing we
can do with python.
• “A File is a collection of characters in which we can perform read
and write functions. And also we can save it in secondary storage.”
Write to file
(Save)
Python External
Program File
(Secondar
y Storage)
Read from
file (Load)
Data File Operations
Following main operations can be done on files -
1. Opening a file
2. Performing operations
1. READ
2. WRITE etc.
3. Closing The File
Here the point is that the file “Hello.txt” which is used here is pre
built and stored in the same folder where Python is installed.
A Program to read
“Hello.txt” File.
Output
Output
1. We can also use readline( ) function which can read one line at a
time from the file.
2. Same readlines( ) function is used to read many lines.
Writing to a File
• We can write characters into file by using following two methods -
1. write (string)
2. writelines (sequence of lines)
• write( ) : it takes a sting as argument and adds to the file. We have to use
‘\n’ in string for end of line character .
• writelines ( ) : if we want to write list, tupleinto the file then we use
writelines ( ) function.
A program to write
in “Hello.txt”
Output
Writing to a File. . .
Output
“Hello.txt” File is
created using the
above program.
Writing to a File.
Hello.txt file is opened using “with”.
Output
“Hello.txt” File is
created using the
above program.
Appending in a File
• Append means adding something new to existing file.
• ‘a’ mode is used to accomplish this task. It means opening a file in
write mode and if file is existing then adding data to the end of the
file.
A program to append
into a file “Hello.Txt”
Output
Student File is
created by using
the above
Output program.
Operations in Binary File.
• If we want to write structure such as list, dictionary etc and also we
want to read it then we have to use a module in python known as
pickle.
• Pickling means converting structure into byte stream before writing
the data into file.
• And when we read a file then a opposite operation is to be done
means unpickling.
• Pickle module has two methods - dump( ) to write and load( ) to read.
Operations in Binary File.
• pickle.dump():
• This function is used to store the object data to the file. It
takes 3 arguments.
• 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. And the third
argument is the key-value argument. This argument defines
the protocol.
• There are two types of protocol –
pickle.HIGHEST_PROTOCOL and
pickle.DEFAULT_PROTOCOL.
• Pickle.load():
• This function is used to retrieve pickled data.
• The primary argument of pickle load function is the file
object that you get by opening the file in read-binary (rb)
mode.
Operations in Binary File
• To read Binary file use of load ( ) function -
Operations in Binary File
Iteration over Binary file - pickle module
import pickle
output_file = open("d:\\a.bin", "wb")
myint = 42
mystring = "Python.mykvs.in!" mylist =
["python", "sql", "mysql"]
mydict = { "name": "ABC", "job": "XYZ" }
pickle.dump(myint, output_file)
pickle.dump(mystring, output_file)
pickle.dump(mylist, output_file)
pickle.dump(mydict, output_file)
output_file.close()
with open("d:\\a.bin", "rb") as f:
while True:
try:
r=pickle.load(f) print(r) print(“Next Read objects
data") one by one
except EOFError:
break
f.close()
Operations in Binary File
Insert/append record in a Binary file - pickle module
rollno = int(input('Enter roll number:'))
name = input('Enter Name:')
marks = int(input('Enter Marks'))