0% found this document useful (0 votes)
16 views

3-file-handling

Uploaded by

sleepyguy1609
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

3-file-handling

Uploaded by

sleepyguy1609
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

File Handling

Data File Handling


• We have seen yet only the transient programs. The programs which run
for a short period of time and give some output and after that their data is
disappeared. And when we again run those programs then we have to use
new data.
• This is because the data is entered in primary memory which is temporary
memory and its data is volatile.
• Those programs which are persistent i.e. they are always in running or run
for a long time then their data is stored in permanent storage (e.g.
harddisk) . If the program is closed or restarted then the data used will be
retrieved.
• For this purpose the program should have the capability to read or write
the text files or data files. These files can be saved in permanent storage.
• The meaning of File I/O (input-output) is to transfer the data from Primary
memory to secondary memory and vice-versa.

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

Open Process Close


File Data File
Beside above operations there are some more operations can be done on
files.-
• Creating of Files
• Traversing of Data
• Appending Data into file.
• Inserting Data into File.
• Deleting Data from File.
• Copying of File.
• Updating Data into File.
File Types
File are of two types –
1. Text File: A text file is sequence of line and line is the
sequence of characters and this file is saved in a permanent
storage device. Although in python default character coding
is ASCII but by using constant ‘U’ this can be converted into
UNICODE. In Text File each line terminates with a special
character which is EOL (End Of Line). These are in human
readable form and these can be created using any text
editor.
2. Binary File: Binary files are used to store binary data such
as images, videos audio etc. Generally numbers are stored
in binary files. In binary file, there is no delimiter to end a line.
Since they are directly in the form of binary hence there is no
need to translate them. That’s why these files are easy and
fast in working.
Opening & Closing Files
• We need a file variable or file handle to work with files in Python.
• This file object can be created by using open( ) function or file( )
function.
• Open( ) function creates a file object, which is used later to access
the file using the functions related to file manipulation.
• Its syntax is following -
<file_object>=open(<file_name>,<access_mode>)
• File accessing modes -
– read(r): To read the file

– write(w): to write to the file Python External


Progra File
– append(a): to Write at the end of file.
m (Second
a ry
Storage
Read )
from file
(Load)
Opening & Closing Files. . .
Opened 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.

The file is closed.

A program describing the functions of file handling.


Output
File Modes
Mode Description
r To read the file which is already existing.
rb Read Only in binary format.
r+ To Read and write but the file pointer will be at the beginning of the file.
rb+ To Read and write binary file. But the file pointer will be at the beginning of
the file.
w Only writing mode, if file is existing the old file will be overwritten else the new
file will be created.
wb Binary file only in writing mode, if file is existing the old file will be overwritten
else the new file will be created.
wb+ Binary file only in reading and writing mode, if file is existing the old file will be
overwritten else the new file will be created.
a Append mode. The file pointer will be at the end of the file.
ab Append mode in binary file. The file pointer will be at the end of the file.
a+ Appending and reading if the file is existing then file pointer will be at the end
of the file else new file will be created for reading and writing.
ab+ Appending and reading in binary file if the file is existing then file pointer will
be at the end of the file else new file will be created for reading and writing.
Reading a File

A Program to read
“Hello.txt” File.

Output

Hello.txt file was


created using
notepad.|
Reading a File . . .
Reading first 10
characters from
the file “Hello.txt”

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”

This “Hello.txt” is created using


above program.

Output
Writing to a File. . .

A Program to use writelines()


function

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

A new data is appended into


Hello.txt by above program.
Writing User Input to the File.

Taking the data from


user and writing this
data to the file
“Stude nt.txt”.

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'))

#Creating the dictionary


rec = {'Rollno':rollno,'Name':name,'Marks':marks}
#Writing the Dictionary
f = open('d:/student.dat','ab') pickle.dump(rec,f)
f.close()
Operations in Binary File
Read records from a Binary file - pickle module
f = open('d:/student.dat','rb')
while True:
try:
rec = pickle.load(f)
print('Roll Num:',rec['Rollno'])
print('Name:',rec['Name'])
print('Marks:',rec['Marks'])
except EOFError:
break
f.close() Here we are creating
dictionary rec to
dump it in
student.dat file
Relative and Absolute Paths
• We all know that the files are kept in directory which are also
known as folders.
• Every running program has a current directory. Which is
generally a default directory and python always see the default
directory first.
• OS module provides many such functions which can be used to
work with files and directories. OS means Operating System.
• getcwd( ) is a very function which can be used to identify the
current working directory.
Methods of OS module
1.The rename() method used to rename the file.
Syntax: os.rename(current_file_name, new_file_name)
2.The remove() method to delete file.
Syntax: os.remove(file_name)
2.The mkdir() method of the os module to create directories
in the current directory.
Syntax: os.mkdir("newdir")
4.The chdir() method to change the current directory.
Syntax: os.chdir("newdir")
4.The getcwd() method displays the current directory.
Syntax: os.getcwd() e.g.program
4.The rmdir() method deletes the directory. import os
print(os.getcwd())
Syntax: os.rmdir('dirname')
os.mkdir("newdir")
5. The os.path.exists() method if the path exists. os.chdir("newdir")
Syntax: os.path.exists(absolute/relative path) print(os.getcwd())
Getting and Resetting file positions
• The tell() method of python tells us the current position within the file
• 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", 'rb+') OUTPUT
print(f.tell()) 0
print(f.read(7)) # read seven characters b'Welcome'
print(f.tell()) 7
print(f.read()) b' to
print(f.tell()) python.mykvs.in\r\nRe
f.seek(9,0) # moves to 9 position from begining gularly visit
print(f.read(5)) python.mykvs.in'
f.seek(4, 1) # moves to 4 position from current location 59
print(f.read(5)) b'o pyt'
f.seek(-5, 2) # Go to the 5th byte before the end b'mykvs'
print(f.read(5)) b'vs.in'
Standard File Streams
• We use standard I/O Streams to get better performance from
different I/O devices.
• Some Standard Streams in python are as follows -
– Standard input Stream sys.stdin
– Standard output Stream sys.stdout
– Standard error Stream sys.stderr
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. When 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
CSV FILE (Comma separated value)
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
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)
Write / Read CSV FILE
• import csv module for file operation/method call.
• For writing, open file in ‘w’ writing mode using open()
method which create new File like object
• Through csv.writer() method, create writer object to call
writerow() method to write objects.
• For reading, open the file in ‘r’ mode and create new File like
object
• create new file reader object using
csv.reader()method to read each row of the file.
• There are three file opening modes: ‘w’, ’r’, ’a’
• After file operation, close the opened file using close()
method.

You might also like