0% found this document useful (0 votes)
3 views55 pages

File Handling

This document provides a comprehensive guide on file handling in Python, covering the creation, reading, writing, and manipulation of text and binary files. It explains the types of files, the use of various file modes, and methods for reading and writing data, as well as file operations like renaming and deleting files. Key functions such as open(), read(), write(), seek(), and tell() are discussed to facilitate effective file management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views55 pages

File Handling

This document provides a comprehensive guide on file handling in Python, covering the creation, reading, writing, and manipulation of text and binary files. It explains the types of files, the use of various file modes, and methods for reading and writing data, as well as file operations like renaming and deleting files. Key functions such as open(), read(), write(), seek(), and tell() are discussed to facilitate effective file management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 55

FILE HANDLING IN

PYTHON
INTRODUCTION TO FILES
We have so far created programs in Python that accept the input,
manipulate it and display the output.
But that output is available only during execution of the program
and input is to be entered through the keyboard. This is because
the variables used in a program have a lifetime that lasts till the
time the program is under execution.
What if we want to store the data that were input as well as the
generated output permanently so that we can reuse it later?
Usually, organisations would want to permanently store
information about employees, inventory, sales, etc. to avoid
repetitive tasks of entering the same data.
Hence, data are stored permanently on secondary storage devices
for reusability. We store Python programs written in script mode
with a .py extension.
Each program is stored on the secondary device as a file. Likewise,
the data entered, and the output can be stored permanently into a
file
INTRODUCTION TO
FILES

So, what is a file?

A file is a named location on a


secondary storage media where
data are permanently stored for
later access.
TYPES OF FILES
• Computers store every file as a collection of 0s and 1s i.e., in binary form.
Therefore, every file is basically just a series of bytes stored one after the
other.

• There are mainly two types of data files — text file and binary file. A text
file consists of human readable characters, which can be opened by any
text editor.

• On the other hand, binary files are made up of non-human readable


characters and symbols, which require specific programs to access its
contents.
TEXT FILES
• A text file can be understood as a sequence of characters consisting of
alphabets, numbers and other special symbols. Files with extensions
like .txt, .py, .csv, etc. are some examples of text files. When we open a text
file using a text editor (e.g., Notepad), we see several lines of text. However,
the file contents are not stored in such a way internally. Rather, they are
stored in sequence of bytes consisting of 0s and 1s.

• In ASCII, UNICODE or any other encoding scheme, the value of each


character of the text file is stored as bytes. So, while opening a text file, the
text editor translates each ASCII value and shows us the equivalent
character that is readable by the human being. For example, the ASCII value
65 (binary equivalent 1000001) will be displayed by a text editor as the
letter ‘A’ since the number 65 in ASCII character set represents ‘A’.
TEXT FILES
• Each line of a text file is terminated by a special character,
called the End of Line (EOL). For example, the default EOL
character in Python is the newline (\n).

• However, other characters can be used to indicate EOL.


When a text editor or a program interpreter encounters
the ASCII equivalent of the EOL character, it displays the
remaining file contents starting from a new line. Contents
in a text file are usually separated by whitespace, but
comma (,) and tab (\t) are also commonly used to
separate values in a text file.
BINARY FILES
• Binary files are also stored in terms of bytes (0s and 1s),
but unlike text files, these bytes do not represent the
ASCII values of characters. Rather, they represent the
actual content such as image, audio, video, compressed
versions of other files, executable files, etc. These files are
not human readable.

• Thus, trying to open a binary file using a text editor will


show some garbage values. We need specific software to
read or write the contents of a binary file.

• Binary files are stored in a computer in a sequence of


bytes. Even a single bit change can corrupt the file and
make it unreadable to the supporting application. Also, it
is difficult to remove any error which may occur in the
binary file as the stored contents are not human readable.
We can read and write both text and binary files through
Python programs.
OPENING READING &
CLOSING FILE
The key function for working with files in Python is the open() function.
The open() function takes two parameters; filename, and mode.
There are four different methods (modes) for opening a file:

"r" - Read - Default value. Opens a file for reading, error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists

EXAMPLE 1:
a = open("C:\\test\pritika.txt")
print(a.read())
a.close()
OPENING READING &
CLOSING FILE
EXAMPLE 2 :

a = open("C:\\test\pritika.txt","a")
a.write("New content")
a.close()

a = open("C:\\test\pritika.txt","r")
print(a.read())
WRITING TO A
TEXT FILE
• For writing to a file, we first need to open it in write or append mode.
If we open an existing file in write mode, the previous data will be
erased, and the file object will be positioned at the beginning of the
file. On the other hand, in append mode, new data will be added at
the end of the previous data as the file object is at the end of the file.
After opening the file, we can use the following methods to write
data in the file :

• write() - for writing a single string


• writeline() - for writing a sequence of strings
WRITING TO A
TEXT FILE
The write() method writes a specified text to the
file.

Where the specified text will be inserted depends


on the file mode and stream position.

"a": The text will be inserted at the current file


stream position, default at the end of the file.

"w": The file will be emptied before the text will be


inserted at the current file stream position, default
0.

a = open("C:\\Pritika\Ex1.txt" , "a")
print(a.write(" goodevening"))
a.close()

On execution, write() returns the number of characters


written on to the file. Hence, 13, which is the length of
the string passed as an argument, is displayed.
WRITING TO A
TEXT FILE
If numeric data are to be written to a
text file, the data need to be
converted into string before writing to
the file.

a = open("C:\\Pritika\Ex1.txt" , "w")
marks = 58
#number 58 is converted to a string
using
#str()
a.write(str(marks))
a.close()
WRITING TO A
TEXT FILE
The 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.

a = open("C:\\Pritika\Ex1.txt" , "w")
str = ["hello everyone \n", "how are you ?" , " I am learning python programming "]
a.writelines(str)
a.close()

On opening myfile.txt, using notepad, its content will appear


READING FROM A TEXT
• Before reading a file,FILE
we must make sure that the
file is opened in “r”, “r+”, “w+” or “a+” mode.
There are three ways to read the contents of a
file:
This method is used to read a specified number of bytes of data
from a data file. The syntax of read() method is:
file_object.read(n)
• The read() method
a = open("C:\\Pritika\Ex1.txt" , "r")
print (a.read(5))
a.close()

If no argument or a negative number is specified in read(), the


entire file content is read.
READING FROM A TEXT
FILE
•The readline([n]) method
•This method reads one complete line from a file where each line terminates
with a newline (\n) character. It can also be used to read a specified number (n)
of bytes of data from a file but maximum up to the newline character (\n).
a = open("C:\\Pritika\Ex1.txt" , "r")
print (a.readline(50))
a.close()

If no argument or a negative number is specified, it reads a


complete line and returns string.

To read the entire file line by line using the readline(), we can
use a loop. This process is known as looping/ iterating over a file
object. It returns an empty string when EOF is reached.
READING FROM A TEXT
FILE
•The readlines() method
•The method reads all the lines and returns the
lines along with newline as a list of strings.

a = open("C:\\Pritika\Ex1.txt" , "r")
print (a.readlines(50))
a.close()
PROGRAM TO WRITE AND READ A
TEXT FILE

fobject=open("testfile.txt","w") # creating a data


file
sentence=input("Enter the contents to be written
in the file: ")
fobject.write(sentence) # Writing data to the file
fobject.close() # Closing a file
print("Now reading the contents of the file: ")
fobject=open("testfile.txt","r")
#looping over the file object to read the file
for str in fobject:
print(str)
fobject.close()
CREATE A TEXT FILE
fp = open('sales.txt', 'x')
fp.close()
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
1. 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:fobject.tell()

2.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])

The seek() function sets the position of a file pointer and the
tell() function returns the current position of a file pointer.
SEEK ()
For example, when you open a file in write mode, the file
pointer is placed at the 0th position, i.e., at the start of the
file. However, it changes (increments) its position as you
started writing content into it.

Or, when you read a file line by line, the file pointer moves
one line at a time.

Sometimes we may have to read only a specific portion of


the file, in such cases use the seek() method to move the
file pointer to that position.

For example, use the seek() function to do the file


operations like: –

Read a file from the 10th character.


Syntax: jump to the 5th character from the end of the file.
Directly
Add new content to file after a particular position.
f.seek(offset, whence)
SEEK ()
The allowed values for the whence argument are: –

A whence value of 0 means from the beginning of the file.


A whence value of 1 uses the current file position
A whence value of 2 uses the end of the file as the reference
point.
The default value for the whence is the beginning of the file,
which is 0
SEEK ()
a = open("C:\\Pritika\Random.txt" , "w+" )
a.write('My First Line\n')
a.write('My Second Line')
a.seek(19,0)
print(a.read())
a.close()
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("We are moving to 10th byte position from
the beginning of
file")
print("The position of the file object is at",
fileobject.tell())
str=fileobject.read()
print(str)
CREATING AND TRAVERSING A TEXT
FILE
• To create a text file, we use the open() method and provide the filename and the mode.
If the file already exists with the same name, the open() function will behave differently
depending on the mode (write or append) used. If it is in write mode (w), then all the
existing contents of file will be lost, and an empty file will be created with the same
name.

To create a text file and write data in it


# program to create a text file and add data
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()
RENAME FILE IN PYTHON
To rename a file, Please follow these steps:

1. Find the path of a file to rename


To rename a file, we need its path. The path is the location of the file on the disk.
An absolute path contains the complete directory list required to locate the file.
A relative path contains the current directory and then the file name.

2. Decide a new name


Save an old name and a new name in two separate variables.
old_name = 'details.txt'
new_name = 'new_details.txt’

3. Use rename() method of an OS module


Use the os.rename() method to rename a file in a folder. Pass both the old name and
a new name to the os.rename(old_name, new_name) function to rename a file.
RENAME FILE IN PYTHON
import os

# Absolute path of a file


old_name = "C:\\Pritika\Random.txt"
new_name = "C:\\Pritika\Test.txt"

# Renaming the file


os.rename(old_name, new_name)

The following are the parameters that we need to pass for the
os.rename() method

src : Path for the file that has to be renamed


dst : A destination path for the newly renamed file
src_dir_fd : (Optional) Source file directory
dst_dir_fd : (Optional) Destination file directory
RENAME A FILE AFTER CHECKING WHETHER
IT EXISTS

The os.rename() method raises the FileExistsError


or OSError When the destination file name already
exists. This can be avoided by wrapping our code
in the try-except block.

Use the os.path.isfile(destination_path) function


before renaming a file. It returns true if the
destination file already exists.

We can use the following two approaches to


continue with renaming by removing the old file or
stop without renaming it.

Use os.path.isfile() in an if condition


Write rename code in the try-except block.
RENAME A FILE AFTER CHECKING WHETHER
IT EXISTS

import os

# Absolute path of a file


old_name = "C:\\Pritika\Random.txt"
new_name = "C:\\Pritika\Test.txt"

if os.path.isfile(new_name):
print("The file already exists")
else:
# Rename the file
os.rename(old_name, new_name)
DELETE (REMOVE) FILES AND DIRECTORIES
IN PYTHON

1. Find the path of a file


We can delete a file using both relative path and
absolute path. The path is the location of the file
on the disk.
An absolute path contains the complete directory
list required to locate the file. And A relative path
includes the current directory and then the file
name.
For example, /home/Pynative/reports/samples.txt
is an absolute path to discover the samples.txt.

Use os.remove() function to delete File


The OS module in Python provides methods to
interact with the Operating System in Python. The
remove() method in this module is used to
remove/delete a file path.
First, import the os module and Pass a file path to
DELETE (REMOVE) FILES AND DIRECTORIES
IN PYTHON

Remove file with relative path

import os

# removing a file with relative path


os.remove("sales_1.txt")

Remove file with absolute path

import os

# remove file with absolute path


os.remove("C:\\Pritika\Random.txt“)
Ques 2

a = open("C:\\Pritika\Test.txt")
str = a.read()
size = len(str)
print("size of file is" , size , "bytes")
Ques 3

a = open("C:\\Pritika\Test.txt")
str = a.readlines()
linecount = len(str)
print("Number of lines in File is : " , linecount)
a.close()
Ques 4
count= int(input(" What is the Total Strenght of
class ? "))

a = open("C:\\Pritika\Test.txt" , 'w')

for i in range(count):
print("Enter details of student", (i+1) , "below")
rollno = int(input("Roll Number"))
name = input("Name:")
marks = float(input("Marks"))

record = str(rollno) + " , " + name + " , " +


str(marks) + '\n'
a.write(record)
a.close()
Ques 5:

file1 = open("C:\\Pritika\Test.txt","r")
str1 = file1.read()

vowel_count = 0
for i in str1:
if( i=='A' or i=='a' or i=='E' or i=='e' or i=='I'
or i=='i' or i=='O' or i=='o'
or i=='U' or i=='u'):

vowel_count +=1

print('The Number of Vowels in text file :',


vowel_count)

file1.close()
BINARY FILES IN
PYTHON
Most of the files that we see in our computer system are called
binary files.
Example:
1.Document files: .pdf, .doc, .xls etc.
2.Image files: .png, .jpg, .gif, .bmp etc.
3.Video files: .mp4, .3gp, .mkv, .avi etc.
4.Audio files: .mp3, .wav, .mka, .aac etc.
5.Database files: .mdb, .accde, .frm, .sqlite etc.
6.Archive files: .zip, .rar, .iso, .7z etc.
7.Executable files: .exe, .dll, .class etc.
BINARY FILE
MODES
THE PICKLE MODULE
During execution of a program, we may require to store current state of
variables so that we can retrieve them later to its present state. Suppose
you are playing a video game, and after some time, you want to close it. So,
the program should be able to store the current state of the game, including
current level/stage, your score, etc. as a Python object. Likewise, you may
like to store a Python dictionary as an object, to be able to retrieve later. 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. Pickling is a method of preserving food items by
placing them in some solution, which increases the shelf life. In other words,
it is a method to store food items for later consumption.

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. 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
THE 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.

DAT files are data files that contain information pertaining to the program that
they're associated with.

import pickle
listvalues=[1,"Geetika",'F', 26]
fileobject=open("mybinary.dat", "wb")
pickle.dump(listvalues,fileobject)
fileobject.close()
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)

import pickle
print("The data that were stored in file are: ")
fileobject=open("mybinary.dat","rb")
objectvar=pickle.load(fileobject)
fileobject.close()
print(objectvar)
FILE HANDLING USING PICKLE
MODULE

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)

import pickle
print("The data that were stored in file are: ")
fileobject=open("mybinary.dat","rb")
objectvar=pickle.load(fileobject)
fileobject.close()
print(objectvar)
Write a program to create a list of 10
numbers and store it in a file “num.dat”

import pickle
L=[]
for i in range(10):
num = int(input("Enter number"))
L.append(num)
f=open("num.dat" , 'wb')
pickle.dump(L,f)
f.close()
#To verify your input, below is the code for
reading file
f=open("num.dat" , 'rb')
d = pickle.load(f)
print(d)
Write a program to write the following dictionary in
a binary file named “detail.dat”.
D = {1 : ‘A’, 2 : ‘B’, 3 : ‘C’}

import pickle

D = {1 : 'A', 2 : 'B', 3 : 'C'}

f=open("detail.dat" , 'wb')

pickle.dump(D,f)

f.close()

#To verify your input, below is the code for


reading file

f=open("detail.dat" , 'rb')

d = pickle.load(f)

print(d)
Write a program to store roll number and marks of
five students in a dictionary and write the
dictionary in a binary file “student.dat”

import pickle
d={ }
for i in range(5):
rno = int(input("Enter Roll number"))
marks = int(input("Enter marks"))
d[rno] = marks
f=open("student.dat" , 'wb')
pickle.dump(d,f)
f.close( )
#To verify your input, below is the code for
reading file
f=open("student.dat" , 'rb')
d = pickle.load(f)
print(d)
import pickle
print("WORKING WITH BINARY FILES")
bfile=open("empfile.dat","ab")
recno=1
print ("Enter Records of Employees")
print()
#taking data from user and dumping in the file as list object
while True:
print("RECORD No.", recno)
eno=int(input("Employee number : "))
ename=input("Employee Name : ")
ebasic=int(input("Basic Salary : "))
allow=int(input("Allowances : "))
totsal=ebasic+allow
print("\tTOTAL SALARY : ", totsal)
edata=[eno,ename,ebasic,allow,totsal]
pickle.dump(edata,bfile)
ans=input("Do you wish to enter more records (y/n)? ")
recno=recno+1
if ans.lower()=='n':
print("Record entry OVER ")
break

# retrieving the size of file


print("Size of binary file (in bytes):",bfile.tell())
bfile.close()
# Reading the employee records from the file using load() module
print("Now reading the employee records from the file")
print()
readrec=1
try:
with open("empfile.dat","rb") as bfile:
while True:
edata=pickle.load(bfile)

print("Record Number : ",readrec)


print(edata)
readrec=readrec+1
except EOFError:
pass
bfile.close()
CSV (COMMA
SEPARATED VALUES)
• CSV (Comma Separated Values) is a
simple file format used to store tabular data, such
as a spreadsheet or database. A CSV file stores
tabular data (numbers and text) in plain text. Each
line of the file is a data record. Each record consists
of one or more fields, separated by commas. The
use of the comma as a field separator is the source
of the name for this file format.
• For working CSV files in python, there is an inbuilt
module called csv.
CSV (Comma Separated Values)
• When Use CSV?

• When data has a strict tabular structure


• To transfer large database between programs
• To import and export data to office applications
• To store, manage and modify shopping cart catalogue
Name Class Section
Amit X A
Sumit XI B
Ashish XII C
CSV (Comma Separated Values)
PYTHON PROVIDES CSV MODULE TO WORK WITH CSV FILE:

MAIN FUNCTIONS ARE:


1.READER()
2.WRITER()
3.DICTREADER()
4.DICTWRITER()

READING FROM CSV FILE :


import csv
f = open(“data.csv”, ‘r’)
row = csv.reader(f)
for i in row :
print(i)
CSV (Comma Separated Values)
There are two main methods used in writing in csv file
1.writerow()
2.writerows()

WRITING TO CSV FILE : import csv


import csv fields = [‘Name’ , ‘Class’ , ‘Subject’]
f = open(“data.csv”, ‘w’) rows = [[“Amit” , ‘XII’ , ‘CS’] ,
wr = csv.writer(f) [‘Sumit’ , ‘X’ , ‘IP’] ,
wr.writerow([‘Name’ , ‘Class’]) [‘Ashu’ , ‘XI’ , ‘CS’]]
wr.writerow([‘Amit’ , ‘XII’]) f = open(“data.csv” , ‘w’ , newline = ‘ ‘)
f.close( ) wr = csv.writer(f)
wr.writerow(fields)
wr.writerows(rows)
f.close
CSV (Comma Separated Values)
Reading a CSV file with DictReader:
This function(DictReader) is working similar to reader(). This function return lines as a
dictionary instead of list.

import csv
f = open(“data.csv” , ‘r’)
row = csv.DictReader(f)
for i in row:
print(i)
PROGRAMS
Q1. Write a program to read entire data from
file data.csv

import csv

f=open("data.csv", 'r')

d=csv.reader(f)

for row in d:

print(row)
Q2. Write a program to add/insert records in file “data.csv”. Structure of a record is
roll number, name and class.
import csv

field = ["Roll no" , "Name" , "Class"]

f = open("data.csv" , 'w')

d=csv.writer(f)

d.writerow(field)

ch='y'

while ch=='y' or ch=='Y':

rn=int(input("Enter Roll number: "))

nm = input("Enter name: ")

cls = input("Enter Class: ")

rec=[rn,nm,cls]

d.writerow(rec)

ch=input("Enter more record??(Y/N)")

f.close()
Q. 3Write a program to copy the data from “data.csv” to
“temp.csv”

import csv

f=open("data.csv","r")

f1=open("temp.csv",'w')

d=csv.reader(f)

d1=csv.writer(f1)

for i in d:

d1.writerow(i)

f.close( )

f1.close( )
Q.4 Write a program to show the detail of the student who scored
the highest marks. Data stored
import csv
in “Data.csv” is given below
Rollno, Name, Marks
1, Aman, 35 f=open("data.csv","r")
2, Kanak, 1
3, Anuj, 33 d=csv.reader(f)
4, suman, 25
next(f)

max=0

for i in d:

if int(i[2])>max:

max=int(i[2])

f.close()

f=open("data.csv","r")

d=csv.reader(f)

next(f)

for i in d:

if int(i[2])==max:

print(i)

f.close()

You might also like