Chapter 5 FileHandlingFinal

Download as pdf or txt
Download as pdf or txt
You are on page 1of 56

Revised as

Computer Science per


CBSE
Curriculum
Class XII (CBSE Board) 2022-23

UNIT-1: Programming & Computational Thinking-2

File Handling in
Python
Syllabus: (Class XII:CS 2022-23)
□ Introduction to files, types of files (Text file, Binary file, CSV
file), relative and absolute paths
□ Text file: opening a text file, text file open modes (r, r+, w,
w+, a, a+), closing a text file, opening a file using with
clause, writing/appending data to a text file using write()
and writelines(), reading from a text file using read(),
readline() and readlines(), seek and tell methods,
manipulation of data in a text file.
□ Binary file: basic operations on a binary file: open using file
open modes (rb, rb+, wb, wb+, ab, ab+), close a binary file,
import pickle module, dump() and load() method, read,
write/create, search, append and update operations in a
binary file.
□ CSV file: import csv module, open / close csv file, write into
a csv file using csv.writer() and read from a csv file using
csv.reader( )
Objective..
In this presentation you will learn about the basic
concepts and operations of File handling in Python
Programming language covering Text file, Binary
File and CSV file handling.
□ Introduction to files & File Handling
□ Various Operations on File
□ Handling Text Files
□ Handling Binary Files
□ Handling .CSV files
□ Applications of file
Introduction
In general, Python program accepts data from
keyboard and stores data in memory variables
temporarily. To store data permanently, we
have store data in data files on secondary
storage. Once the data stored in the files , we
can access data and reuse in the application.
FILE HANDLING is a mechanism by which we can
access and manipulate data files (create/read/
write/update) stored on secondary storage
media using Python programs and library
functions.
Data File & Python Program

Writing File DATA Reading


(Program to FILES File
File) (File to
Program)

Python
Program

Data file contains data belong to a specific application.


Data File can stored as Text File, Binary File or CSV File.
Types of data file
In general, data is stored in the series of binary
numbers (0s and 1s) on the disk. However these
byte stream can be represented as characters in
the applications.
In view of storage structure and representation, a
data file can be categorized as-
▪ Text File: Data is represented as series of
characters.
▪ Binary File: Data is represented as collection of
bytes.
▪ CSV File: Similar to text file but data value is
separated with comma (,)
Text File
▪ Text file stores information as a series of characters
(alphabets, numbers and special symbols) in ASCII
OR UNICODE format.
▪ Text file is simplest format and can be created
through any Text Editor applications like Note Pad.
▪ In text file each line is terminated by special
character called EOL (End of line) marker. In python
EOL is presented as ‘\n’ or ‘\r’ or combination of
both. File is ended by EOF (End of File) character.
▪ In text file, characters are represented in characters
through translation done by text editor.
Binary files
▪ Binary files stores the information in the series of Bytes
as stored in the memory i.e. data is stored according to
its data type and no translation/conversion occurs.
▪ Data in binary files cannot be directly read using any Text
Editor program. It can be read only through program
through which it is created.
▪ 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.
▪ The .exe files, .mp3 file, image files are some of the
examples of binary files.
.CSV Files (Comma Separated values)
▪ CSV file is a simple text in which data values are
separated by comma(,).
▪ Generally, .CSV files are used for importing/
Exporting data from one database application to
others.
▪ These types of files are used as general structure
and one line represents one record.
▪ These files can be created using Text Editors
(Note Pad, MS Word), Spreadsheet program (MS
Excel) etc.
Text File Vs Binary File
Text File Binary File
Data is represented as series Data is represented as group of
of characters. Bytes as stored in the memory.
Store only plain text in a file. Can store different types of data
(audio, text, image) in a single
file.
Commonly used file format Developed for an application and
and can be opened in any can be opened in that application
text editor. only.
Less chance to get corrupted Can easily get corrupted, corrupt
as change can be undone. on even single bit change.

Mostly .txt and .rtf are used May have any extension as
as extensions to text files. defined by the application.
Data File Handling in Python
OPENING FILE
We should first open the file for read or write
operation by specifying the name of file and
access mode.
PERFORMING READ/ WRITE OPEARTION
Once the file is opened, we can either read or
write data as per requirement, using various
library functions available in Python.
CLOSING FILE
After performing operation, we must close the
file and release the file for other application to
use it.
Opening a File
A file can be opened for read, write, append operation.
file_object = open(filename, mode)
Where file_object is file handler and mode represents
access mode of file. If mode is not given then default
mode is “read”
Example: myfile = open(“story.txt”)
In this example disk file “story.txt” is loaded in
memory for READ operation and its reference is linked
to “myfile” object.
It is assumed that “story.txt” is stored in the same
folder where .py file is stored otherwise, we have to
give full path (location) of the file.
Location of file: File PATH
▪ PATH is term which refers to location of file on the
storage media.
▪ A file Path can be written as –
DRIVE :\ FOLDER (DIRECTORY)\....\FILE.
▪ In general, the hard disk is logically divided into
many parts called DRIVES like C: , D: etc. The
drive is the main container which holds all Folders
and Files.
▪ Drive is also known as ROOT DIRECTORY.
▪ Drive contains Folder and Files.
▪ Folder contains sub-folders or files.
▪ Files are the actual data container.
Absolute Path & Relative Path
Absolute path is the full address of any file or folder
starting from the Drive i.e. from ROOT -
DriveName:\Folder1\Folder2\…..\filename
for e.g. the absolute path of file STORY.TXT may
be- C:\MyFolder\2022\June\STORY.TXT
Relative Path is the location of file/folder in respect of the
current folder. The following special symbols are used:
▪ Single Dot (.) : single dot (.) refers to current folder.
▪ Double Dot (..) : double dot (..) refers to parent folder
▪ Backslash (\) : backslash refers to ROOT folder.
for e.g. if absolute path of file STORY.TXT is
C:\MyFolder\2022\June\STORY.TXT and you are currently
working in 2022 folder then Relative path can be written as-
.\June\STORY.TXT
Opening File using PATH
In general, if path is not given the Python assumes
that file is to be created/accessed from location where
program file (.py) is kept. If data file is stored
elsewhere, we can specify path string.
myfile = open(“d:\\myFolder\\2022\\June\\Story.txt”)

While giving path, we must use double backslash(\\)


in place of single backslash because in python single
slash is used to represent escape characters.
For example, if the folder name is “nitin” and we
provide path as d:\nitin\poem.txt then '\n' in the
path string may represent newline character (‘\n’).
Opening File using PATH
myfile = open(“d:\\myFolder\\poem.txt”)
Alternatively, we can use “r” letter before
the path making the string as raw string
i.e. no special meaning attached to any
character. So above command can be
written as-
myfile = open(r “d:\myFolder\poem.txt”)
File Access Mode in Python
Text File Binary File Description Notes
Mode Mode
‘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’ Read and File must exists otherwise error is raised
‘rb+’ write Both reading and writing can take place
w+ ‘w+b’ Write and File is created if not exists, if exists data
‘wb+’ read will be truncated, both read and write
allowed

‘a+’ ‘a+b’ Write and Same as above but previous content will
‘ab+’ read be retained and both read and write.
Opening File using “With” Statement:
❖ Python’s “with” statement for file handling is very handy
when you have related operations which you would like
to execute as a block of code in between:
with open(filename[, mode]) as filehandle:
file_manipulation_statement
Example:
with open(“story.txt”, “w”) as mf:
mf.write(“Once upon a time”)
❖ The advantage of “with” clause is that it will
automatically close the file after executing the nested
block of code.
Closing file
When a data file is open, its reference or File
handle is stored on File_Object on which file
is opened. So, after performing operations
on the file, the data file must be closed.
A file can be closed using close() function
through its file handler object.

myfile.close()

Note: open function is built-in function used


standalone while close() must be called
through file handle.
Reading from
File
Python provide many functions to read a file.
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 End of line (EOL) character or blank
string if no more bytes are left for reading.
Filehandle.readlines() :
Reads all lines and returns them in a list instead
of string.
Questions on Reading
Operations:
Most of the problems asked in CBSE Board exam
covers the following types of reading operations-
Problems related to Character Reading :
Read file character by character and perform
operation. Ex: Counting frequency of a letter.
Problems related to word reading:
Read file and extract words and perform
operations. Ex: Counting frequency of a word.
Problem related to line reading:
Read the file and perform line wise operations.
Ex: counting line starting with vowels.
Reading character by character
#Ex1: Reading whole file & print
mf=open("data.txt","r")
Str=mf.read()
print (str)

#Ex-2: Reading file character by character


mf=open("data.txt","r")
str=mf.read()
#Ex-2: Counting Vowels in the file
for ch in str:
mf=open("data.txt","r")
print(ch)
str=mf.read()
count=0
for ch in str:
if ch in [‘a’,’e’,’i’,’o’,’u’]:
Basic Structure of
count=count+1
the program
print(count)
Reading line by line
#Ex: reading line by line
mf=open(“data.txt”,”r”)
str=mf.readline()
while str:
print(str)
str=mf.readline()
mf.close()

#Ex: reading line starting #Ex: reading line having


# with “P” letter # “Prayagraj” word
mf=open("data.txt","r") mf=open("data.txt","r")
str=mf.readline() str=mf.readline()
while str: while str:
if str[0]=="P": if “Prayagraj” in str:
print(str) print(str)
str=mf.readline() str=mf.readline()
mf.close() mf.close()
Reading words and all lines
#Ex: reading all lines
mf=open("data.txt","r")
lst=mf.readlines()
for line in lst:
print(line)
mf.close()

#Ex: counting frequency of Prayagraj word


mf=open("data.txt","r")
str=mf.read()
Wlist=str.split()
count=0
for w in Wlist:
if w==“Prayagraj:
count=count+1
print(count)
mf.close()
Reading file in random way: Seek() & tell()
function

In general text files are accessed in sequential manner.


To read text file in random way, Python provides
seek() and tell() function to manipulate file pointer.
File_object.tell() :
This functions returns current position of the file
pointer in the file. It returns total bytes from the
beginning to the current position of the file.
File_object.seek(offset [,reference]) :
Seek() function moves file pointer for given bytes
(offset) with given reference. Reference value (default
0) can be:
0: from beginning, 1: Current position, 2: End of file.
Example : tell() & seek()
#seek() and tell()
mf=open("data.txt","r+")
str=mf.read(10)
print(str)
print("current position of the file object is: ",mf.tell())
mf.seek(0,2)
print("current position of file pointer is:",mf.tell())
mf.seek(0)
str=mf.read()
print(str)
Important Questions…
Q1. WAP to read the content of a text file and find frequency count
of a letter ‘r’ or ‘R’ .
Q2. WAP to read the content of a file and display how many
uppercase characters and digits are present.
Q3. WAP a program to count number of vowels present in the file.
Q4. WAP to count number of lines starting with ‘a’ letter.
Q5. WAP to count number of worlds in a text file.
Q6. WAP to display odd lines from the text file.
Q7. WAP to display lines starting with ‘a’ letter.
Q8. WAP to display lines ending with ‘n’ letter.
Q9. WAP to display longest line stored in a file.
Q10. Write a function WORDCOUNT() in Python to read the
content of file “xyz.txt”, and a word to be counted as argument
and display the occurrence of given word.
Writing to text
files
After read operation, let us take an example of how
to write data in disk files. Python provides
functions:
• write ()
• writelines()
The above functions are called by the file handle to
write desired content.
Name Syntax Description
write() Filehandle.write(str1) Writes string str1 to file
referenced by filehandle
Writelines() Filehandle.writelines(L) Writes all string in List L as
lines to file referenced by
filehandle.
Example-1:

Observe that
while writing
data to file using
“w” mode the
previous
SECOND RUN.. content of
existing file will
be overwritten.
should write
using “a” mode
i.e. append
mode
Example-2:

New content is
added after
previous content
Example-3: Using
writelines()
Example-4:Writing String as a record to file
Example-5: To copy the content of one file to another file.
Working with Binary files
▪ If we want to write a structured data such as
list or dictionary to a file and read it
subsequently, we need to handle Binary Files
using Python Pickle module.
▪ Pickling is the process of converting structure
to a byte stream before writing to a file.
▪ While reading the content of file a reverse
process called Unpickling is used to convert
the byte stream back to the original format.
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)
Example: load()
# Program to create binary file operations
import pickle
mf=open(“project.dat”,”wb”)
dict1= {1:”a”, 2:”b”, 3: “c”}
pickle.dump(dict1,mf)
mf.close()

# Program to read binary file operations


import pickle
mf=open(“project.dat”,”rb”)
dict1= pickle.load(mf)
print(dict1)
print(“Item 1 is “,dict[1])
mf.close()
Example: Storing employee details in binary
file
Un-Pickling – Reading and Display
Record
Insert/append record
Reading records from a binary file
Searching Record in Binary file
Updating Binary file
Deleting Record from Binary File
Board Exam Questions 2014-15
Board Exam Questions 2016-17
Board Exam Questions 2018-19
Handling CSV Files
◻ CSV (comma-separated values) is a simple file format used to
store tabular data, such as a spreadsheet or database.
◻ Files in the CSV format can be imported to and exported from
programs that store data in tables, such as Microsoft Excel or
OpenOffice Calc.
◻ A comma-separated values file is a delimited text file that uses a
comma to separate values.
◻ 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
◻ To perform read and write operation with CSV file, we must
import csv module.
◻ open() function is used to open file, and return file object.
How to create CSV file
◻ Method 1 (From MS-Excel):
𑰀 Open Excel, delete all the sheet except sheet 1

𑰀 Type all the data, in separate cells


𑰀 Save it as csv file in your desired location.

𑰀 If any warning comes, click on "YES"

𑰀 Now file is created at your desired location, go and double


click or open with notepad to check the content
How to create CSV file
◻ Method 2 (From Notepad):
𑰀Open Notepad
𑰀 Type record by separating each column value by
comma(,)
𑰀 Every record in separate line

𑰀 Save it by giving extension .csv (PUT THE NAME IN


DOUBLE
QUOTES TO ENSURE .TXT WILL NOT BE APPENDED
WITH
FILE NAME FOR E.G. if you want it to save with name
emp then give name as “emp.csv” in double quotes
𑰀 File is created close it and double click to open and
check
Writing data in CSV file
◻ import csv module
◻ Use open() to open CSV file by specifying
mode “w” or “a”, it will return file object.
◻ “w” will overwrite previous content
◻ “a” will add content to the end of previous
content.
◻ Pass the file object to writer object with
delimiter.
◻ Then use writerow() to send data in CSV file
Example -1: Writing data to CSV
file
myfile.c
sv
Example-2 : appending data to CSV file

myfile.csv
BEFORE
EXECUTION

myfile.csv
OUTP AFTER
UT EXECUTION
Writing record using writerow()
Writing records using writerows()
Searching Record in CSV File
Thanks…

You might also like