0% found this document useful (0 votes)
37 views15 pages

Chapter 2

Chapter 2 covers file handling in Python, including types of files (text and binary), basic operations (reading, writing, appending), and file modes. It explains how to open and close files, the use of the 'with' clause for automatic closure, and functions for reading and writing data. Additionally, it discusses setting offsets in files using seek() and tell() functions, along with examples of creating and traversing text files.

Uploaded by

rajvyuva79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views15 pages

Chapter 2

Chapter 2 covers file handling in Python, including types of files (text and binary), basic operations (reading, writing, appending), and file modes. It explains how to open and close files, the use of the 'with' clause for automatic closure, and functions for reading and writing data. Additionally, it discusses setting offsets in files using seek() and tell() functions, along with examples of creating and traversing text files.

Uploaded by

rajvyuva79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Chapter 2: File Handling in Python

Chapter 2: File Handling in Python


Topics
• Introduction to Files
• Types of Files
• Opening and Closing a Text File
• Writing to a Text File
• Reading from a Text File
• Setting Offsets in a File
• Creating and Traversing a Text File
• The Pickle Module

Introduction to Files
• A file is a named location on a secondary storage media like hard disk, pen drive where data
are permanently stored for later access.
• Each program is stored on the secondary device as a file.
• Without files, the output is stored temporarily in RAM. If a new program is run, previous
program goes out of RAM.
• Thus, storage in files allows us to store and retrieve the data according to requirements
of the programmer.

There are mainly two types of data files –


1. text file
2. binary file
A text file consists of human readable characters, which can be opened by any text editor
Binary files are made up of non-human readable characters and symbols, which require specific
programs to access its contents.

Text File:
• 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.
• Content in the file are stored in sequence of bytes consisting of Os and Is that represent
ASCII value of characters.
• So, while opening a text file, the text editor translates each ASCII value and shows us the
equivalent character.
• For example, the ASCII value 65 will be displayed by a text editor as the letter 'A΄

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

• Each line of text file is terminated by EOL(End of line) character.


• After writing a new line and enter key is pressed, a newline character is added.
• Example: A file containing Hi and Bye in separate lines stores ASCII values of H, i, \n, B, y
and followed by e.

Binary File:
• The type of files which stores information in the same format as it is held in memory. Stores
information in stream of bytes which does not represent the ASCII values of characters
• Stores data 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.
• Even a single bit change can corrupt the file and make it unreadable to the
supporting application

Text File Binary File


A text file consists of human readable A binary file is made up of non-human
characters, which can be opened by any text readable characters and symbols, which
editor. require specific programs to access its
contents.

A text file is a file that stores information in the A binary file is a file that stores the information
form of a stream of ASCII or Unicode in the form of a stream of bytes.
characters.

In text files, each line of text is terminated with In a binary file, there is no delimiter for a line
a special character known as EOL (End of Line) and no character translations occur here.
character.

Files with extensions like .txt, .py, .csvetc are Files with extensions like .jpg, .pdf etc are some
some examples of text files. examples of binary files.

Basic Operations on a File


The basic operations on a file are :
• Reading data from file: The process of extracting already existing data from external file and
displaying through python program.
• Writing data onto file: The process of inserting new data onto a blank external file or already
existing file
• Appending data onto file: If new data is added to a file which already contains some data, it
is called appending.

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

File Modes:
It specifies the type of operation performed after opening the file. Ex: read, write etc
Path: It is defined as sequence of directory names which gives the hierarchy to access a particular
file. It is of two types:
i) Absolute Path: If location of file is specified from root directory.
Ex: C:\\Users\\Desktop\\Demo\\a.txt
ii) Relative Path: If location of file is specified from current working directory. No need to give full
path
Ex: if cwd is C:\\Users\\Desktop . Then relative path to text file is a.txt.

Opening and Closing a Text File


• In real world applications, computer programs deal with data coming from different sources
like databases,CSV files, HTML, XML, JSON, etc.
• We broadly access files either to write or read data from it.
• But operations on files include creating and opening a file, writing data in a file, traversing a
file, reading data from a file and so on.
• Python has the io module that contains different functions for handling files

Opening a Text File:


• Files are opened using the open() function which returns a file object.
• File object is defined as a reference to a file on disk. It serves as a connection link between
the programs and file.

Syntax:
file_object = open("filename", "filemode")
Examples: f = open("myfile.txt") opens the file in read mode.
File pointer is at the beginning. Optional to specify'r mode
f = open("myfile.txt", "w") opens file in Science file in write only mode.
The file_object has certain attributes that tells us basic information about the file, such as:
• <file.closed> returns true if the file is closed and false otherwise.
• <file.mode> returns the access mode in which the file was opened.
• <file.name> returns the name of the file.

Using double slash :


Ex: x = open("C:\\Demo\\a.txt")

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

Using raw path:


Ex: x = open(r"C:\Demo\a.txt")

File Attributes:
fileobject.closed
True if file is closed
fileobject.mode
The access_mode is an optional argument that represents the mode in which the file has to
be accessed by the program

#Access mode of file


fileobject.name
Name of the file

Programming Example with relative path


f = open("welcome.txt")
if f:
print(" The file successfully opened")

Output:
The file successfully opened

Programming Example with absolute path


f = open("C:/Users/sunil/Videos/welcome.txt")
if f:
print(" The file successfully opened")

Output:
The file successfully opened

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

Closing a Text File:


Once we are done with the read/write operations on a file, it is a good practice to close the file to
avoid memory leaks.
Syntax:
fileobject.close()
Opening a file using with clause:
The advantage of using with clause is that any file that is opened using this clause is closed
automatically.
Syntax: with open("filename", "file mode") as fileobject:
Example: with open("C:\\Demo\\a.txt", "r") as f:
Here f is the file object.
Programming Example
f = open("welcome.txt")
if f:
print(" The file successfully opened")

f.close() #closing file

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

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, once the control comes outside the with clause. In case the user forgets to close the
file explicitly or if an exception occurs, the file is closed automatically. Also, it provides a simpler
syntax.
with open(“myfile.txt”,”r+”) as myObject:
content = myObject.read()
Here, we don’t have to close the file explicitly using close() statement. Python will automatically
close the file.

Functions for Reading and Writing from Text File

Writing to a Text File:


• For writing to a text file, we first need to open it in write or append mode. (w, w+, r+, a, a+)
• If file is opened in write mode, existing data is vanished and file object is at beginning of file.
Iast position.
• If opened in append mode, existing data is retained and new data will be added to end of
file.
The functions for writing data to text file are:
• write()
• writelines()

write() method:
• This function is used to insert string data onto an external file.
• It returns the number of characters being written on single execution.
• If data is in different format, it needs to be converted to string using str()
Syntax: file_object.write(string)
Program:

f = open('C:/Users/sunil/Videos/welcome.txt',mode = 'w')
f.write('SHIKSHANASIRI ACADEMY – PUC SCIENCE & COMMERCE COACHING')
print("data added successfully")
f.close()

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

writelines() method:
• writelines() function is used to write multiple strings to a file in the form object like lists,
tuple, etc.
• Unlike write(), the writelines() method does not return the number of characters written in
the file.
Syntax: file_object.writelines(LIST/TUPLE)
Program:

f = open('C:/Users/sunil/Videos/welcome.txt',mode = 'w')
f.writelines(['SHIKSHANASIRI ACADEMY.','Learn python, java and C++ \n', 'SSLC - PUC Coaching
Classes\n'])
print("data added successfully")
f.close()

Reading data from Text File:


Reading data from text file means extracting already existing data from external file and displaying
through python program.
The necessary condition is that the file must exist. File can be opened in r, r+, w+ and a+ modes.
The functions for reading data from text file includes:
1. read()
2. readline([n])
3. readlines()

read():
This function is used to read 'n' bytes of data from an existing file.
Syntax: fileobject.read(n)
If no argument or a negative number is specified in read(), the entire file content is read
Program:

f = open('C:/Users/sunil/Videos/welcome.txt', "r")
print(f.read())
f.close()

Output:
SHIKSHANASIRI ACADEMY. Learn python, java and C++

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

readline(n)): optional
This function is used to read one line of input at a time ending with \n from an existing file.
It can also be used to read a specified number (n) of bytes of data from a file.
Syntax: fileobject.readline(n) //n is optional
If no argument or a negative number is specified in read(), the entire line is read.
Program:

f = open('C:/Users/sunil/Videos/welcome.txt', "r")
print(f.readline(21))
f.close()

Output:
SHIKSHANASIRI ACADEMY

readlines():
This function is used to read all lines from a text file and return the result in the form of list.
Syntax: fileobject.readlines()
Program:

f = open('C:/Users/sunil/Videos/welcome.txt', "r")
print(f.readlines())
f.close()

Output:
['SHIKSHANASIRI ACADEMY.\n', 'Learn python, java and C++ \n', 'SSLC - PUC Coaching
Classes\n']

Programming Example

f = open('C:/Users/sunil/Videos/welcome.txt', "r")
d = f.readlines()
for line in d:
words=line.split()
print(words)
f.close()

Output:
['SHIKSHANASIRI', 'ACADEMY.']

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

['Learn', 'python,', 'java', 'and', 'C++']


['SSLC', '-', 'PUC', 'Coaching', 'Classes']

Program:
f = open('C:/Users/sunil/Videos/welcome.txt', "r")
d = f.readlines()
for line in d:
words=line.splitlines()
print(words)
f.close()

Output:
['SHIKSHANASIRI ACADEMY.']
['Learn python, java and C++ ']
['SSLC - PUC Coaching Classes']

Program: Writing and reading to 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()

Setting Offsets in a File


If we want to access data in a randomly, then Python gives us seek() and tell() functions.
• tell() function: This function returns an integer that specifies the current position of the file
object in the file.(file pointer)
Syntax: fileobject.tell()
• seek() function: It is used to take the file pointer to a specified position from reference point
( 0 = start, 1 = current, 2 = end).
Syntax: file_object.seek(offset [, reference_point])
offset is the number of bytes by which the file object is to be moved.

Program 2-2 Application of seek() and tell()


print("Learning to move the fi le object")
fileobject=open("testfi le.txt","r+")

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

str=fileobject.read()
print(str)
print("Initially, the position of the fi le object is: ",fi leobject. tell())
fileobject.seek(0)
print("Now the fi le object is at the beginning of the fi le: ",fileobject.tell())
fileobject.seek(10)
print("We are moving to 10th byte position from the beginning of file")
print("The position of the fi le object is at", fi leobject.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.
• if the file is created in append mode (a), then the new data will be written after the existing
data.
• In both cases, if the file does not exist, then a new empty file will be created.

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): ")
ifans=='n': break
fileobject.close()

Output of Program 2-3:


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

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

Creating a Text File and Writing Data & Displaying


To create a text file, we use the open() method.

• If file is opened in write mode, existing data is vanished and file object is at beginning of file.
• If opened in append mode, existing data is retained and new data will be added to end of
file.
• If file doesn't exist, it is newly created in both cases.
• Traversing is the process of reading text line by line.
Program: 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()

Output of Program 2-3:


>>>
RESTART: Path_to_file\Program2-3.py
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

Traversing a file and displaying data


Traversing a file means reading its contents line by line and processing the data. This can be done
using different file reading methods like read(), readline(), readlines(), or simply iterating over the
file object. The file will be opened in read mode and reading will begin from the beginning of the
file.
Program: To display data from a text file

fileobject = open("practice.txt","r+")
str = fileobject.readline()
while str:
print(str)
str = fileobject.readline()
fileobject.close()

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

Output:
I am interested to learn about Computer Science. Python is easy to learn

Program: To perform reading and writing operation in a text file

fileobject=open("report.txt", "w+")
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)
fileobject.close()

Output:
RESTART: Path_to_file\Program2-5.py
WRITING DATA IN THE FILE
Enter a sentence I am a student of class XII
Do you wish to enter more data? (y/n): y
Enter a sentence my school contact number is 4390xxx8
Do you wish to enter more data? (y/n): n
The byte position of file object is 67

READING DATA FROM THE FILE


I am a student of class XII
my school contact number is 4390xxx8

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

The Pickle Module


• The pickle module in Python is used for serialization and deserialization objects, allowing
you to save Python objects to a file and reload them later. This is useful for storing complex
data structures like lists, dictionaries, or even custom objects.
• 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.
• 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.
• Pickling(serialization) is the process of converting python object into bytestream. (Writing) .
• Unpickling(deserialization) is the process of converting bytestream back to python
object. (Reading)

Functions related to pickle module:


1) dump(): used to write a python object onto a binary file (.dat file)
Syntax: pickle.dump(<python object>, <file object>)
Program: Pickling data in Python
import pickle
listvalues=[1,"Geetika",'F', 26]
fileobject=open("mybinary.dat", "wb")
pickle.dump(listvalues,fileobject)
fileobject.close()

2) load(): used to read data from binary file (.dat file). It raises EOF Error when when you reach end
of file. Thus we must handle the exception
Syntax: variable = pickle.load(<fileobject>)
Program: 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)

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

File handling using pickle module


Program 2-8 accepts a record of an employee from the user and appends it in the binary file tv.
Thereafter, the records are read from the binary file and displayed on the screen using the same
object. The user may enter as many records as they wish to. The program also displays the size of
binary files before starting with the reading process.

Program: To perform basic operations on a binary file using pickle module


# Program to write and read employee records in a binary file
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("\tEmployee number : "))
ename=input("\tEmployee Name : ")
ebasic=int(input("\tBasic Salary : "))
allow=int(input("\tAllowances : "))
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 ")
print()
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:

SUNIL D N | KSVK PU COLLEGE


Chapter 2: File Handling in Python

while True:
edata=pickle.load(bfile)
print("Record Number : ",readrec)
print(edata)
readrec=readrec+1
except EOFError:
pass
bfile.close()

Output of Program 2-8:


RESTART: Path_to_file\Program2-8.py
WORKING WITH BINARY FILES
Enter Records of Employees
RECORD No. 1
Employee number : 11
Employee Name : D N Ravi
Basic Salary : 32600
Allowances : 4400
TOTAL SALARY : 37000
Do you wish to enter more records (y/n)? y
RECORD No. 2
Employee number : 12
Employee Name : Farida Ahmed
Basic Salary : 38250
Allowances : 5300
TOTAL SALARY : 43550
Do you wish to enter more records (y/n)? n
Record entry OVER
Size of binary file (in bytes): 216
Now reading the employee records from the file
Record Number : 1
[11, 'D N Ravi', 32600, 4400, 37000]
Record Number : 2
[12, 'Farida Ahmed', 38250, 5300, 43550]

SUNIL D N | KSVK PU COLLEGE

You might also like