You are on page 1of 260

SYLLABUS OUTLINE

 Data File Handling:


 Need for a data file,
 Types of data files – Text file and Binary file;
 Text File:
 Basic file operations on text file:
 Creating/Writing text into file,
 Reading and
 manipulation of text from an already existing text File (accessing sequentially);
 Binary File:
 Creation of file,
 Writing data into file,
 Searching for required data from file,
 Appending data to a file,
 Insertion of data in sorted file,
 Deletion of data from file,
 Modification of data in a file;
Introduction
 All programs we looked earlier:
 input data from the keyboard.
 output data to the screen.
 Difficult to handle large amount of input data.

 Output would also be lost as soon as we exit


from the program.
 How do we store data permanently?.

 We can use secondary storage device.


 Data is packaged up on the storage device as data
structures called files.
Files

 Files are used to store data in a relatively


permanent form, on floppy disk, hard disk, tape
or other form of secondary storage.
 Files can hold huge amounts of data if need be.
 Ordinary variables (even records) are kept in
main memory which is temporary and rather
limited in size.
Why use files?

• Convenient way to deal with large


quantities of data.
• Store data permanently (until file is
deleted).
• Avoid having to type data into program
multiple times.
• Share data between programs.
Data File Handling Concepts

File. The information / data stored under a specific


name on a storage device, is called a file.

Record: It is a collection related information about


a certain entity( student)

Field: It is an individual information( variables)


admno, name, class, sec, address etc
Flow of Data….

Data
PROGRAM
(Records to (records
be to be
extracted) inserted)
DEVICES OR
FILES
Data
Types of Files

 The two basic types of files are


 Text files
&
 Binary files
Diff. b/w text mode and Binary mode

 A text file consists of readable characters.


 Text files can be manipulated by any text editors.
 A text file is simply a sequence of ASCII or Unicode
characters.
 Each line of text is terminated with a special
character known as EOL character .
 Some internal translations takes place when this
EOL character is read or written.
 By default EOL character is (‘\n’)
Diff. b/w text mode and Binary mode

 A binary file stores data to disk in the


same form in which it is represented in
main memory.
 No translations takes place.
 No delimiters for the line.
 Binary files are faster and easier for a
program to read and write than text files..
Diff. b/w text mode and Binary mode

 In Text mode file, character


translations takes place. So it is slower
in processing than binary files.
 No translation takes place. So binary
files are more efficient and faster in
processing.
Text Files Binary Files

 In a text file, we  When using a


write out each binary file we write
piece of data about whole record data
a given record to the file at once.
separately.

 but the numbers in


 The text file will be the binary file will
readable by an not be readable in
editor this way.
The programs to create the data files will differ in how they
open the file and in how they write to the file.

 for text file, we use the  For binary file we use


usual write(), dump() and load()
writelines() to store methods of pickle
record separately. module to write and
 with text file, we read read from file
each record from the respectively.
file by using read(),
readline(), readlines()
functions
The programs to create the data files will differ in how they
open the file and in how they write to the file.

 By default mode of file  For the binary file we


is TEXT mode. No need will use ‘b’ to
for mentioning it while create/open the file.
we are creating/opening
the file
Steps for file operations

1. Opening/ creating file

2. Doing any file operations


 Reading records
or
 Writing records

3. Closing file
Opening and Closing Files

 To perform file operation ,it must be opened first then reading, writing,
editing operation can be performed.
 To create any new file also, it must be opened.
 On opening of any file ,a file relevant structure is created in memory as
well as memory space is created to store contents.
 Once we are done working with the file, we should close the file. Closing
a file releases valuable system resources.
 In case we forget to close the file, Python automatically closes the file
when program ends or file object is no longer referenced in the program.
 However, if our program is large and we are reading or writing multiple
files, then it can take significant amount of resources on the system. If we
keep opening new files carelessly, we could run out of resources. So to
be a good programmer , we should close the file as soon as all tasks
are done with it.
OPENING A FILE by OPEN() method

Before any reading or writing operation of any


file, it must be opened first.

Python provides a built in function open()


for it.

On calling, this function creates file object


for file operations.
OPENING A FILE by OPEN() method

object_name = open(filename)
OR
object_name = open(filename, mode)
OR
with open(“filename”,”mode”) as object_name:

object_name is an identifier called link to file


filename is the name of file to be opened ( it is a string)
mode is read(r) / write(w) / append(a)
OPENING A FILE by OPEN() method

f1=open(“t1.txt”)
or
f1=open(“t1.txt”,”r”) # for read only mode
or
f1=open(“t1.txt”,”w”) # for write only mode
or
f1=open(“t1.txt”,”a”) # for append mode
File Access Modes
Text Binary Descrip- Notes
file File tion
modes Modes
r rb Read only File must exist already. It sets the pointer at the beginning
of the file. This is the default mode.
w wb Write If not exist, it creates otherwise opens file for write.
only Previous contents will be deleted as we open existing file

a ab append Same as write only, new data will be added at end


without deleting previous ones. If the file does not exist, it
creates a new file.

r+ rb+ Read and Opens for both read and write modes. File pointer is
write placed at the beginning of the file.

w+ wb+ Read and Opens for both read and write modes. If no file exists,
write then it creates a new file for reading and writing.

a+ ab+ Read and Opens for both read and append modes. If no file exists,
append then it creates a new file for reading and writing. File
pointer is placed at the end of the file.
Closing of File

object_name.close()
e.g. fl.close()
where fl is the object/link to file

Note : There is no need to give the physical file


name at the time of closing a file.
File Object Attributes

 closed: It returns true if the file is closed and false when the
file is open.
 encoding: Encoding used for byte string conversion.
 mode: Returns file opening mode
 name: Returns the name of the file which file object holds.
 newlines: Returns “\r”, “\n”, “\r\n”, None or a tuple
containing all the newline types seen.
E.g. Program
OUTPUT
f = open("a.txt", 'a+')
print(f.closed) False
print(f.encoding) cp1252
print(f.mode) a+
print(f.newlines) None
print(f.name)) a.txt
File Object Attributes

F1.closed # Returns True if file is closed


otherwise False
F1 .mode # Returns mode of opening of file
F1 .name # Returns name of file

Note : F1 is the object of a file.


Writing Data into file
1. write()
object.write(string) it writes a string into data file

f1=open(‘t1.txt’ , ‘w’)
str=input( “enter data “)
f1.write(str)
Send your name and roll number in a text file
named demo
File demo as a notepad file created in last program
Writing Data into file
2. writelines()
object.writelines(L) it writes a list of strings
into data file

f1=open(‘t1.txt’)
Lstr = [‘neha’ , ‘aman’ , ‘ria’]
f1.writelines(Lstr) # Lstr is a list of strings
Send your name and roll number in a text file
named demo
WHAT HAPPENS IF WE SKIP THE f1.close( ) statement

file demo is created but nothing gets written in that file.


READING DATA FROM THE FILE
1. read()
object.read() or object.read(n) # n is number of bytes
it reads either first ‘n’ bytes of data or in absence of ‘n’ whole data to
be retrieved.

f1 = open(‘t1.txt’)
data = f1.read()
print (data)

Or

f1=open(‘t1.txt’)
data=f1.read(10) # it reads first 10 bytes
print(data)
Without parameters, read() retrieves all data
from the file in the form of string.
With parameters, read() retrieves n number of
bytes from the file, where n is passed as
parameter to the function.
If the value of n is greater than the no. of bytes
in the file, then it reads all the data from the
file and ignores the value of n.
2. readline()
object.readline() or
object.readline(n) # n is number of characters

it reads either one line of text or first ‘n’ characters of data .


Returns in the form of strings

f1=open(‘t1.txt’)
data=f1.readline()
print(data)

Or

f1=open(‘t1.txt’)
data=f1.readline(10) # it reads first 10 characters
print(data)
Sending
multiple
records
in the
file
Without parameters, readline() retrieves one
line of data from the file in the form of string.

readline() reads the ‘\n’ at


the end of line and prints it
when one line is read, i.e.
readline() is used without
parameters.
With parameters, readline() retrieves n bytes
from the file, where n is passed as parameter
to the function.
3. readlines()
object.readlines() : it reads all line of text .
Returns in the form of lists

f1=open(‘t1.txt’)
data=f1.readlines() # data is a list
print(data)
Without parameters, readlines() retrieves all
lines of data from the file in the form of list of
strings.
WAP to send n records in a file, and also retrieve
and print the file contents in a single program.
of = open("data1.txt","w")
n=int(input("Enter no. of entries : "))
for i in range(n):
name = input("Enter name : ")
age = input("Enter age : ")
of.write(name)
of.write("\t")
of.write(age)
of.write("\n")
of.close()

f = open("data1.txt","r")
record = f.readlines()
for l in record:
print(l)
f.close()
Storing data using with clause and
retrieving data
n = int(input("Enter no. of records : "))
with open("data2.txt","w") as fobj:
for i in range(n):
name=input("Enter name : ")
age =input("Enter age : ")
fobj.write(name)
fobj.write("\t")
fobj.write(age)
fobj.write('\n')
#no need of close() required

f=open("data2.txt","r")
rec=f.read()
print(rec)
f.close()
Storing data using with clause and
retrieving data (using try-except block)
n = int(input("Enter no. of records : "))
with open("data2.txt","w") as fobj:
for i in range(n):
name=input("Enter name : ")
age =input("Enter age : ")
fobj.write(name)
fobj.write("\t")
fobj.write(age)
fobj.write('\n')
#no need of close() required

try :
f=open("data2.txt","r")
rec=f.read()
print(rec)
f.close()
except IOError :
print("No File Found...")
Storing data by writelines() and retrieving data
using read()
WAP to create a file original.txt that contains n numbers. Send even numbers from this
file to even.txt and odd numbers to odd.txt. Print both the files after creation.

n=int(input(" how many numbers"))


of=open("original.txt","w")
for i in range(n):
num=input(" enter any number ") oddf=open("odd.txt","r")
of.write(num)
of.write("\n") print(" Odd numbers are\n")
of.close() num=oddf.readlines()
for i in num:
oddf=open("odd.txt","w")
evenf=open("even.txt","w") print(i,end=" ")
iff=open("original.txt","r") oddf.close()
num=iff.readlines()
for i in num:
if int(i) %2==0:
evenf.write(i) evenf=open("even.txt","r")
evenf.write("\n") print(" Even numbers are\n")
else:
oddf.write(i) num=evenf.readlines()
oddf.write("\n") for i in num:
oddf.close() print(i,end=" ")
evenf.close()
iff.close() evenf.close()
ORIGINAL
EVEN
10
11 10
12 12
13 14
14

ODD

11
13

NUM = [ 10 , 11 , 12 , 13 , 14 ]
WAP to send multiple lines in a file.
with open('text1.txt', 'w') as f1:
while True:
line=input('Enter line of text : ')
if len(line)==0:
break
else:
f1.write(line + '\n')

f2 = open("text1.txt" , 'r')
data = f2.read()
print("*** File Contents ***")
print(data)
f2.close()
Assume a file text.txt already exists that contains
multiple lines of text.
WAP to count no. of characters in the file.

iff = open("text.txt","r")
data=iff.read()
print("***File Contents***")
print(data)
l=len(data)
print("No. of characters in file : ",l)
iff.close()
Assume a file text.txt already exists that contains
multiple lines of text.
WAP to count no. of vowels in the file.

iff = open("text.txt","r")
count=0 Method 1 :
# by default it reads a line of text checking "\n" Without
for x in iff: using read()
for ch in x:
if ch.lower() in ['a','e','i','o','u']:
count+=1

print('Number of vowels : ',count)


iff.close()
Assume a file text.txt already exists that contains
multiple lines of text.
WAP to count no. of vowels in the file.

iff = open("text.txt","r")
count=0
data = iff.read() Method 2 :
for x in data: With read()
if x.lower() in ['a','e','i','o','u']:
count+=1

print('Number of vowels : ',count)


iff.close()
Assume a file text.txt already exists that contains multiple lines of
text. WAP to count no. of times a particular character appears in
the file.

iff = open("text.txt","r")
count=0
ch = input("Enter the character to count : ")
data = iff.read()
for x in data:
if x == ch:
count+=1

print('Number of times', ch ,'appears = ',count)


iff.close()
Assume a file text.txt already exists that contains multiple lines of
text. WAP to count no. words in the file.

iff = open("text.txt","r")
data = iff.read()
words = data.split()
count = len(words)
print('Number of words in file = ',count)
iff.close()
Assume a file text.txt already exists that contains multiple lines of
text. WAP to count no. of specific words in the file.
iff = open("text.txt","r")
data = iff.read()
words = data.split()
wd = input("Enter the word to count : ")
count = 0

for x in words:
if x == wd :
count += 1

print('Number of times %s appears in file = %d'%(wd,count))


iff.close()
Assume a file text.txt already exists that contains multiple lines of
text. WAP to count no. of times word ‘me’ appears in the file.
iff = open("text.txt","r")
data = iff.read()
words = data.split()
count = 0

for x in words:
if x == ‘me’ :
count += 1

print('Number of times %s appears in file = %d'%(wd,count))


iff.close()
Create a file text1.txt that contains multiple lines of text. WAP to
read data from the file and display each word separately.

#Writing to the file


with open('text1.txt', 'w') as f1:
while True:
line=input('Enter line of text : ')
if len(line)==0:
break
else:
f1.write(line + '\n')

#Reading from the file


try:
f1=open('text1.txt','r')
lines = f1.readlines()
print("*** File contents ***")
for l in lines:
for w in l.split():
print(w)
f1.close()
except:
print('File not found')
Assume a file text1.txt already exists that contains
multiple lines of text.
WAP to read data from the file and display each word
separated along with ‘*’.

try:
f1=open('text1.txt','r')
lines = f1.readlines()
print("*** File contents ***")
for l in lines:
for w in l.split():
print(w , end = ' * ')
f1.close()
except:
print('File not found')
Assume a file text.txt already exists that contains multiple lines of text.
WAP to count no. of times word ‘he’ and ‘his’ appears in the file.
iff = open("text.txt","r")
data = iff.read()
words = data.split()
count1 = count2 = 0

for x in words:
if x == 'he' :
count1 += 1
elif x == 'his' :
count2 += 1

print('Number of times "he" appears in file = %d'%(count1))


print('Number of times "his" appears in file = %d'%(count2))

iff.close()
Assume a file text.txt already exists that contains multiple lines of
text. WAP to count no. of lines in the file.

iff=open("text.txt","r")
count=0 Method 1 :
without
# by default it reads a line of text checking "\n" using read()
for n in iff:
count += 1
print("No. of lines in the file : ",count)
iff.close()
Assume a file text.txt already exists that contains multiple lines of
text. WAP to count no. of lines in the file.

iff=open("text.txt","r")
count=0 Method 2:
with
lines = iff.readlines() readlines()
count = len(lines)

print("No. of lines in the file : ",count)


iff.close()
Assume a file text.txt already exists that contains multiple lines of
text. WAP to display each line from the file in reverse order.

try:
with open('text.txt','r') as f1:
L=f1.readlines()
for i in L:
print(i[::-1], end=' ')
except FileNotFoundError:
print('File Not Found')
Assume a file text.txt already exists that contains multiple lines of
text. WAP to display each word from the file in reverse order.

try:
with open('text.txt','r') as f1:
lines = f1.readlines()
for line in lines:
words = line.split()
for x in words :
print(x[::-1], end=' ')
print()
except FileNotFoundError:
print('File Not Found')
Assume a file text.txt already exists that contains multiple lines of
text. WAP to count number of lines starting with ‘T’.

Using try
try:
except block
count = 0
with open('text.txt','r') as f1:
lines = f1.readlines()
for line in lines:
if line[0] == 'T':
count += 1
print("No. of lines starting with 'T' = ",count)
except FileNotFoundError:
print('File Not Found')
Assume a file text.txt already exists that contains multiple lines of
text. WAP to count number of lines starting with ‘T’.

without try
count = 0
except block
with open('text.txt','r') as f1:
lines = f1.readlines()
for line in lines:
if line[0] == 'T':
count += 1
print("No. of lines starting with 'T' = ",count)
Assume a file text.txt already exists that contains multiple lines of
text. WAP to count number of lines starting with ‘I’ or ‘i'.

count = 0
f1 = open('text.txt','r')
lines = f1.readlines()
for line in lines:
if line[0].lower() == 'i':
count += 1
print("No. of lines starting with 'i' or 'I' = ",count)
Assume a file text.txt already exists that contains multiple lines of
text. WAP to count number of lines ending with ‘Y’ or ‘y'.

Method 1

count = 0
f1 = open('text.txt','r')
lines = f1.readlines()
for line in lines:
length = len(line)
if line[length-2].lower() == 'y':
count += 1
print("No. of lines ending with 'y' or 'Y' = ",count)
Assume a file text.txt already exists that contains multiple lines of
text. WAP to count number of lines ending with ‘Y’ or ‘y'.

Method 2

count = 0
f1 = open('text.txt','r')
lines = f1.readlines()
for line in lines:
length = len(line)
if line[length-2] in ['y' , 'Y'] :
count += 1
print("No. of lines ending with 'y' or 'Y' = ",count)
Assume a file text.txt already exists that contains multiple lines of text.
WAP to count number of alphabets, digits and spaces in the file.

Method – 1

iff=open('text.txt','r')
count1=count2=count3=0

lines = iff.read()

for x in lines :
if x.isalpha() :
count1 += 1
elif x.isdigit() :
count2 += 1
elif x == ' ' :
count3 += 1

print("No. of alphabets : ",count1)


print("No. of digits : ",count2)
print("No. of spaces : ",count3)
Assume a file text.txt already exists that contains multiple lines of text.
WAP to count number of alphabets, digits and spaces in the file.

Method – 2

iff=open('text.txt','r')
count1=count2=count3=0
lines = iff.readlines()

for element in lines :


for x in element :
if x.isalpha() :
count1 += 1
elif x.isdigit() :
count2 += 1
elif x == ' ' :
count3+=1

print("No. of alphabets : ",count1)


print("No. of digits : ",count2)
print("No. of spaces : ",count3)
Assume a file text.txt already exists that contains multiple lines of text.
WAP to count number of alphabets, digits and spaces in the file.

Method 3 :
try:
with open("text.txt","r") as iff:
nalpha,ndigit,nspace=0,0,0
line=iff.readlines()
for l in line:
for c in l:
if c.isalpha():
nalpha+=1
elif c.isdigit():
ndigit+=1
elif c== ' ':
nspace+=1
print("No. of alphabets : ",nalpha)
print("No. of digits : ",ndigit)
print("No. of spaces : ",nspace)

except FileNotFoundError:
print('File not found')
Assume a file text.txt already exists that contains multiple lines of text.
WAP to count number of alphabets, digits and spaces in the file.

Method 4 :
try:
with open("text.txt","r") as iff:
nalpha,ndigit,nspace=0,0,0
Using isspace()
line=iff.readlines() counts \t and \n
for l in line:
for c in l: also
if c.isalpha():
nalpha+=1
elif c.isdigit():
ndigit+=1
elif c.isspace():
nspace+=1
print("No. of alphabets : ",nalpha)
print("No. of digits : ",ndigit)
print("No. of spaces : ",nspace)

except FileNotFoundError:
print('File not found')
Write a method/function AEDISP() in python to read lines from a text file WRITER.TXT, and
display those lines, which are starting either with A or starting with E.

For example:

If the content of the file is

________________________________________________________________

A CLEAN ENVIRONMENT IS NECESSARY FOR OUR GOOD HEALTH.

WE SHOULD TAKE CARE OF OUR ENVIRONMENT.

EDUCATIONAL INSTITUTIONS SHOULD TAKE THE LEAD.

________________________________________________________________

The method should display

A CLEAN ENVIRONMENT IS NECESSARY FOR OUR GOOD HEALTH.

EDUCATIONAL INSTITUTIONS SHOULD TAKE THE LEAD.


def AIDESP( ) :
iff=open("writer.txt","r")
count=0
try:
line=iff.readlines()
for x in line:
if x[0]=='A' or x[0]=='E' :
count+=1
print('Number of lines starting with A or E : ' ,count)
iff.close()
except :
print('File not found')

AIDESP()
Write a program to count the number of vowels, digits and special
characters in a text file named ‘data.txt’
try:
with open("text.txt","r") as iff:
nvowels , ndigit , nspecial = 0,0,0
line=iff.readlines() Spaces and \n
for l in line:
for c in l:
are also counted
if c.isalpha() : as special
if c.lower() in ['a','e','i','o','u']:
nvowels+=1
characters
elif c.isdigit():
ndigit+=1
else :
nspecial+=1
print("Number of vowels : ",nvowels)
print("Number of digits : ",ndigit)
print("Number of special characters : ",nspecial)
except FileNotFoundError:
print('File not found')
Write a function AMCount() in Python, which should read each character of a text file text.txt, should
count and display the occurence of alphabets ‘A’ and ‘M’ (including small cases ‘a’ and ‘m ‘too).
Example: If the file content is as follows:
Updated information As simplified by official websites.
The AMCount() function should display the output as: A or a = 4, M or m =2

def AMCount() :
f = open("text.txt" , 'r')
d = f.read()
cnta = cntm = 0
for i in d :
if i.lower() == 'a' :
cnta += 1
elif i.lower() == 'm' :
cntm += 1
f.close()
print("a appears %d times" %cnta)
print("m appears %d times" %cntm)

AMCount()
Write a method/function DISPLAYWORDS() in python to read lines from
a text file STORY.TXT, and display those words, which are less than 4
characters.

def DISPLAYWORDS() :
f = open("text.txt" , 'r')
d = f.read()
words = d.split()
print("** Words with length < 4 characters **")
for i in words :
if len(i) < 4 :
print(i)
f.close()

DISPLAYWORDS()
Write a function RevText() to read a text file “ Story.txt “ and Print only
word starting with ‘I’ in reverse order . Example: If value in text file is:
INDIA IS MY COUNTRY Output will be: AIDNI SI MY COUNTRY.

def RevText() :
f = open("text.txt" , 'r') Method 1 : using
d = f.read()
words = d.split() read()
print("** Required Output **") It displays each
for i in words :
if i[0].upper() == 'I' : word in next line
print(i[::-1] )
else:
print(i)
f.close()

RevText()
def RevText() :
f = open("text.txt" , 'r')
lines = f.readlines() Method 2 : using
readlines()
print("** Required Output **")
for line in lines :
words = line.split()
for word in words :
if word[0].upper() == 'I' :
print(word[::-1] , end = ' ' )
else:
print(word , end = ' ')
print()
f.close()

RevText()
def RevText() :
f = open("text.txt" , 'r')
print("** Required Output **")
while True:
line = f.readline() Method 3 : using
if not line:
break
readline()

words = line.split()
for word in words :
if word[0].upper() == 'I' :
print(word[::-1] , end = ' ' )
else:
print(word , end = ' ')
print()
f.close()

RevText()
WAP to send multiple lines a file in Unicode format and
also read & display data
(without mentioning Unicode during reading)

with open('text1.txt', 'w' , encoding='utf-16') as f1:


while True:
line=input('Enter line of text : ')
if len(line)==0:
break
else:
f1.write(line + '\n')

f2 = open("text1.txt" , 'r' )
data = f2.read()
print("*** File Contents ***")
print(data)
WAP to send multiple lines a file in Unicode format and
also read & display data
(with mentioning Unicode during reading)

with open('text1.txt', 'w' , encoding='utf-16') as f1:


while True:
line=input('Enter line of text : ')
if len(line)==0:
break
else:
f1.write(line + '\n')

f2 = open("text1.txt" , 'r' , encoding='utf-16' )


data = f2.read()
print("*** File Contents ***")
print(data)
Create a file text.txt that contains multiple # copying and displaying
lines of text. iff = open("text.txt","r")
WAP to read data from the file and copy the off = open("text1.txt","w")
contents to another file after capitalizing the
first letter of every line . for j in range(n):
str1 = iff.readline()
str2 = str1[0].upper()
#Writing to the file i=1
n = int(input("Enter no. of records : ")) while i < len(str1):
with open('text.txt', 'w') as f1: if str1[i] != '.':
for i in range(n): str2 = str2 + str1[i]
line=input('Enter line of text : ') else:
f1.write(line + '\n') str2 = str2 + '.' + str1[i+1].upper()
i+=1
i+=1
off.write(str2)
iff.close()
off.close()

iff=open("text1.txt","r")
print("*** Contents in new file ***")
for i in range(n):
str1=iff.readline()
print(str1)
iff.close()
MENU DRIVEN PROGRAM ON TEXT FILES TO PERFORM
THE FOLLOWING OPERATIONS

 INSERTION OF RECORDS

 DISPLAY RECORDS

 SEARCHING

 UPDATION

 DELETION
def menu():
while(True):
print("\n\n ** Menu **")
print("1 : Add")
print("2 : Display all")
print("3 : Search")
print("4 : Count")
print("5 : Modify")
print("6 : Delete")
print("0 : Exit")
ch=int(input("Enter choice : "))
if ch==1:
add()
elif ch==2:
display()
elif ch==3 :
search()
elif ch==4 :
count()
elif ch==5 :
modify()
elif ch==6 :
delete()
elif ch==0 :
break
else:
print("Invalid choice... Try Again...")

menu()
def add() :
f = open("records.txt",'w')
while True:
print("\n")
admno = input("Enter admno : ")
name = input("Enter name : ")
clas = input("Enter class : ")
per = input("Enter percentage : ")
f.write(admno)
f.write("\t")
f.write(name)
f.write("\t")
f.write(clas)
f.write("\t")
f.write(per)
f.write("\n")

ch = input("Add more records? (Y/N) : ")


if ch=="n" or ch=="N":
break
f.close()
def display():
f=open("records.txt","r")
x=f.readlines()
f.close()

print("\n\n *** Student Records ***")


print("Admno \t Name \t Class \t Percentage")

for i in x:
j = i.split()
print(j[0] , j[1] , j[2] , j[3] , sep = ' \t ')
def search():
f=open("records.txt","r")
x=f.readlines()
f.close()
print("\n\n *** Records whose percentage is greater than 75 ***")
for i in x:
j = i.split()
if int(j[3]) >= 75:
print("Admno : ",j[0])
print("Name : ",j[1])
print("Class : ",j[2])
print("Percentage : ",j[3])
print()
def count():
f=open("records.txt","r")
cnt=0
x=f.readlines()

for i in x:
j = i.split()
for letter in j[1]:
if letter == "a":
cnt+=1

print("\n\n *** Count of a in name field ***")


print("Number of times a appears : ",cnt)
def modify():
f = open("records.txt","r")
x = f.readlines()

index=0
for i in x:
j = i.split()
j[3] = int(j[3]) + 3

newdata = str(j[0])+'\t'+j[1]+'\t'+str(j[2])+'\t'+str(j[3])+'\n'
x[index] = newdata

index+=1

f.close()

f=open("records.txt","w")
f.writelines(x)
f.close()
print("*** Percentage of Records incremented by 3 ***")
display()
def delete():
f = open("records.txt","r")
x = f.readlines()

index=0
for i in x:
j = i.split()

if int(j[3])<33 :
x.pop(index)
index +=1

f.close()

f=open("records.txt","w")
f.writelines(x)
f.close()
print("*** Records with percentage<33 Deleted ***")
display()
Storing data by writelines() and retrieving data
using read() : output question
n=int(input(" how many entries"))
l=[]
with open("data3.txt","w") as fobj:
for i in range(n):
name=input(" enter name ")
l+=name
l+= "\n"
fobj.writelines(l)
#no need of close() required

try:
f=open("data3.txt","r")
rec=f.read(10) # reads first 10 characters
rec=f.read() # reads 11th character onwards
print(rec)
f.close()
except IOError:
print("no file exist")
Binary
files
Binary files
 Till now we have written lines/strings in a file.
 But there may be situations, when we need to write and read
non-simple objects like dictionary, tuple, list etc. into files.
 Some of these objects have a structure and hierarchy
associated with their storage.
 In order to maintain this structure and hierarchy, we often
store these objects in binary files with the help of a technique
called serialization.
Binary files

They store data in the binary format (0’s


and 1’s) .
In Binary files there is no delimiter for a
line.
To open files in binary mode, when
specifying a mode, add 'b' to it.
Binary files
 File should be opened in binary mode
(rb/wb/ab/rb+/wb+/ab+)
 To store/retrieve data into a binary file
pickling/unpickling operations will take place.
Use pickle module
Use dump/load functions under pickle module
to store /retrieve data into binary file.
Use of any object( normal data
type/string/list/tuple/dictionary) or class
objects for storing/retrieve data
Binary files

Serialization / Pickling
 It is the process of converting python object
hierarchy into a byte stream so that it can be written
into a file.
 Pickling converts an object in byte form in such a
way that it can be reconstructed in original form
when unpickled or de-serialised.
Binary files

De-Serialization / Unpickling
 It is the inverse of pickling where a byte stream is
converted into an object hierarchy.
 Unpickling produces the exact replica of the original
object.
Binary files
 Python provides pickle module to read and write
objects in a binary file.
 Pickle module implements a fundamental but
powerful algorithm for serializing and de-serializing
python object structure.
 In order to work with pickle module, we must first
import it in our program.
Binary files
 To import pickle module, we write the following
statement :
import pickle
STEPS OF OPERATIONS IN BINARY FILES

1. import pickle module.


2. open binary file in the required mode.
3. process binary file by reading/writing
objects using pickle module’s methods.
4. close the file.
OPENING BINARY FILE

Binary file is opened in the same way as text


files. We just need to mention the appropriate
file mode.

Syntax :
file handle = open (name of file , mode)
FUNCTIONS IN PICKLE MODULE

dump ( )
it is used to write data in the binary file.
Syntax :
pickle.dump (object to be written , file handle)
FUNCTIONS IN PICKLE MODULE

load ( )
it is used to read data from binary file.
Syntax :
object = pickle.load (file handle)
Syntax in Binary Files
1. Opening files
dfile=open(“stu.dat”,”wb”)
or
dfile=open(“stu.dat”,”rb”)
or
with open(“stu.dat”, “wb”) as f:

3. Closing files
dfile.close()

2. For writing
pickle.dump(object to be written, file_handler)

2. For reading
object=pickle.load(file_handler)
Write data to a Binary File

import pickle
e={'Namita':25000,'Manya':30000,'Tanu':20000}
f1=open('emp.dat’ , 'wb’)
pickle.dump( e , f1 )
f1.close()
Read data from a Binary File

import pickle
f1 = open('emp.dat' , 'rb')
e = pickle.load(f1)
print("* Data in file *")
for x in e:
print(x)
f1.close()
Any type of data can be written in a binary file

import pickle
f1 = open("records", "wb")
myint = 42
mystring = "Hello, world!"
mylist = ["dog", "cat" ]
mydict = { "name": "Bob", "job": "Astronaut" }

pickle.dump (myint , f1)


pickle.dump (mystring , f1)
pickle.dump (mylist , f1)
pickle.dump (mydict , f1)

print("** File Created **")


f1.close()
Reading different types of data written in a
binary file

import pickle
f1 = open("records", "rb") Data is read by load()
myint = pickle.load(f1)
mystring = pickle.load(f1)
in the same format as
mylist = pickle.load(f1) it was sent.
mydict = pickle.load(f1)

print("myint = " , myint)


print("mystring = " , mystring)
print("mylist = " , mylist)
print("mydict = " , mydict)
f1.close()
WAP to store roll no and name in a binary file
in the form of a list
import pickle
with open ('data1.dat','wb') as f:
roll = int(input("Enter roll no. : "))
name = input("Enter name : ")
lst = [roll,name]
pickle.dump( lst , f)

with open ('data1.dat','rb') as f1:


lst1= pickle.load(f1)
print("** Data read from the file **")
print(lst1)
Data is read by load()
in the same format as
it was sent.
WAP to store roll no and name in a binary file
in the form of a dictionary

import pickle
with open ('data1.dat','wb') as f:
roll = int(input("Enter roll no. : "))
name = input("Enter name : ")
d = {'ROLL' : roll , 'NAME' : name }
pickle.dump( d , f)

with open ('data1.dat','rb') as f1:


d1= pickle.load(f1)
print("** Data read from the file **")
print(d1)
WAP to store name and age of n people in a
binary file in the form of a dictionary
import pickle
n = int(input("Enter no. of records : ")) Method 1 :
d = {} printing
with open ('data1.dat','wb') as f: dictionary
for i in range(n): without
name = input("Enter name : ") formatting
age = int(input("Enter age : "))
d[name] = age
pickle.dump( d , f)

with open ('data1.dat','rb') as f1:


d1 = pickle.load(f1)
print("** Data read from the file **")
print(d1)
WAP to store name and age of n people in a
binary file in the form of a dictionary
import pickle
n = int(input("Enter no. of records : ")) Method 2 :
d = {} printing
with open ('data1.dat','wb') as f: dictionary with
for i in range(n): formatting
name = input("Enter name : ")
age = int(input("Enter age : "))
d[name] = age
pickle.dump( d , f)

with open ('data1.dat','rb') as f1:


d1 = pickle.load(f1)
print("** Data read from the file **")
for x in d1:
print(x , d1[x] , sep = ' : ')
for i in l:
WAP to copy the if i%2==0:
l1.append(i)
contents of one file into else:
l2.append(i)
another
pickle.dump (l2 , oddf)
import pickle pickle.dump (l1 , evenf)
n=int(input("Enter no. of records : ")) oddf.close()
of=open("original.txt","wb") evenf.close()
l=[] iff.close()
for i in range(n):
num=int(input("Enter any number : ")) oddf=open("odd.txt","rb")
l.append(num) print("** Contents of odd file **")
l1 = pickle.load(oddf)
pickle.dump ( l , of ) for i in l1:
of.close() print(i,end=" ")
oddf.close()
oddf = open("odd.txt","wb")
evenf = open("even.txt","wb") evenf=open("even.txt","rb")
iff = open("original.txt","rb") print("\n ** Contents of even file **")
l2 = pickle.load(evenf)
l1=[] for i in l2:
l2=[] print(i,end=" ")
l = pickle.load(iff) evenf.close()
Sending numbers one by one in the binary file and then
reading them.

import pickle
n=int(input("Enter no. of records : "))
In binary files, load() reads
# storing the data in the same way as
of=open("orig.txt","wb") it is sent in file using dump().
for i in range(n):
num=int(input("Enter any number : ")) If we send numbers one by
pickle.dump(num,of)
of.close() one using dump(), then
load() will read them one by
#displaying one only.
iff=open("orig.txt","rb")
print("**Data read from the file**")
for i in range(n):
num = pickle.load(iff)
print(num,end=" ")
iff.close()
Create a binary file with the name ’member.dat’ and stores
admission number, name and percentage of the child record by
record.
import pickle
with open('member.dat','wb') as f:
while True:
admno=int (input("Enter admission number : "))
name = input("Enter name : ")
per =float(input("Enter percentage : "))
pickle.dump( [admno,name,per] , f)
c=input("Do you want to add more records(Y/N) : ")
if c not in ['y' , 'Y']:
break;
print("** File created **")
WAP to read data from the binary file with the name ’member.dat’
created in last question.

import pickle
Since the data has been sent one by
with open("member.dat","rb") as f: one in last program, it will be read one
by one using load().
print("** Contents of file **")
Also, we do not have a fixed number
while True: of entries that have been sent in file in
last program, so we do not have a
try:
finite number to run the loop.
R=pickle.load(f)
In this case, we need to keep on
print(R) reading the file till it throws an
exception when there is no more data
except: in the file.
break This makes it mandatory to handle
exception using try except block during
reading.
Create a binary file with the name ’member.dat’ and stores
admission number, name and percentage of the child. Store all
records in one go
import pickle
lst = [ ]
with open('member.dat','wb') as f:
while True:
admno=int (input("Enter admission number : "))
name = input("Enter name : ")
per =float(input("Enter percentage : "))

lst.append( [admno , name , per] )


c=input("Do you want to add more records(Y/N) : ")
if c not in ['y' , 'Y']:
break;
pickle.dump( lst , f)
print("** File created **")
WAP to read data from the binary file with the name ’member.dat’ created
in last question. (records will be read in one go as they were sent in one go)

import pickle Since the data has been sent


altogether as a nested list in last
with open("member.dat","rb") as f: program, it will be read as a nested list
using load().
print("** Contents of file **")
Also, since the whole data will be read
R=pickle.load(f) at once, we do not need any loop to
read the data. Only one load()
print(R) statement will read the entire nested
list of data.
R is a nested list of records. It is clear
from the output.
WAP to read data from the binary file with the name ’member.dat’ created
in last question. (records will be read in one go as they were sent in one go)

import pickle Same as last program , but here we


will be displaying the records read in
with open("member.dat","rb") as f: the form of a nested list in a more
formatted manner.
print("** Contents of file **")
R = pickle.load(f)
print("Admno \t Name \t Percentage")
for rec in R:
for x in rec :
print(x , end = ' \t ')
print()
WAP to append more records in the file ’member.dat’ created in last
question. (all records sent in one go)

import pickle
lst = []
with open('member.dat','rb+') as f:
lst = pickle.load(f) #Reading records already in file
while True:
admno=int (input("Enter admission number : "))
name = input("Enter name : ")
per =float(input("Enter percentage : "))

lst.append( [admno , name , per] )


c=input("Do you want to add more records(Y/N) : ")
if c not in ['y' , 'Y']:
break;
f.seek(0)
pickle.dump( lst , f)
print("** More records added in the file **")
By printing the records from the file, it
is evident that a new record has been
appended in the file.
Consider the file ’member.dat’ created in last question. WAP to search for a
record in the file whose admission number is given by the user.

import pickle
x = int(input("Enter admission number to search : "))
flag = 0
with open("member.dat","rb") as f:
R = pickle.load(f)
for rec in R:
if rec[0] == x:
flag=1
print(rec)

if flag == 0 :
print("Record Not Found")
Printing the searched record in formatted manner
Consider the file ’member.dat’ created in last question. WAP to count the
no. of records whose percentage is greater than or equal to 90.

import pickle

count = 0
with open("member.dat","rb") as f:
R = pickle.load(f)
for rec in R:
if rec[2] >= 90:
count += 1

print("No. of records whose percentage > 90 are",count)


Consider the file ’member.dat’ created in last question. WAP to modify the
details of the student whose admission number is entered by the user.
import pickle
try:
with open("member.dat","rb+") as f:
R = pickle.load(f)
ano = int(input("Enter admno to modify : "))
for L in R:
if ano == L[0]:
flag = 1
print(L)
c = input("Do u want to modify(Y/N) : ")
if c in ['y', 'Y'] :
L[1]= input("Enter new name : ")
L[2]=float(input("Enter new per : "))
print("** Record Modified **")
break
f.seek(0)
pickle.dump(R,f)

if flag == 0 :
print("** Record Not Found **")
except:
print("File Not found ")
Printing the updated records
Consider the file ’member.dat’ created in last question. WAP to delete the
details of the student whose admission number is entered by the user.
import pickle

ad=int(input("Enter admission Number to delete : "))


Method 1 :
try:
f1 = open('member.dat','rb+')
using del
data = pickle.load(f1)
i=0
while i < len(data):
if ad == data[i][0]:
print(data[i])
c = input("Do you want to delete record(y/n) : ")
if c in ['y','Y']:
del(data[i])
print(" Record deleted")
i+=1
f1.seek(0)
pickle.dump(data,f1)
f1.close()
except EOFError:
print("Exception Handled")
Printing the updated records
import pickle
try:
with open("member.dat","rb+") as f:
R = pickle.load(f)
ano = int(input("Enter admno to delete : "))
for L in R: Method 2 :
if ano == L[0]:
flag = 1
using
print(L) remove()
c = input("Do u want to delete(Y/N) : ")
if c in ['y', 'Y'] :
R.remove(L)
print("** Record Deleted **")
break
f.seek(0)
pickle.dump(R,f)

if flag == 0 :
print("** Record Not Found **")
except:
print("File Not found ")
Printing the updated records
A binary file “Book.dat” has structure [BookNo, Book_Name, Author, Price].

a) Write a user defined function CreateFile() to input data for a record and add to Book.dat

b) Write a function CountRec(Author) in Python which accepts the Author name as parameter and
count and return number of books by the given Author are stored in the binary file “Book.dat

import pickle
def createfile():
fobj = open("Book.dat","ab")
while True :
BookNo = int(input("Enter Book Number : "))
Book_name = input("Enter book Name : ")
Author = input("Enter Author name : ")
Price = int(input("Price of book : "))
rec = [BookNo, Book_name ,Author, Price]
pickle.dump(rec, fobj)

ch = input("Do you want to enter more(y/n) : ")


if ch not in ['y' , 'Y']:
break
fobj.close()
def countrec(Author):
fobj = open("Book.dat", "rb")
num = 0
try:
while True:
rec = pickle.load(fobj)
if Author == rec[2]:
num = num + 1
print(rec[0],rec[1],rec[2],rec[3])
except:
fobj.close()
return num

createfile()
nm = input("Enter author name to search books : ")
n=countrec(nm)
print("Total records with author name",nm," = ", n)
A binary file “STUDENT.DAT” has structure [admission_number, Name, Percentage].

a) Write a user defined function CreateFile() to input data for a record and add to student.dat

b) Write a function display() that displays all the records of the file

c) Write a function countrec() in Python that would read contents of the file “STUDENT.DAT” and
display the details of those students whose percentage is above 75. Also display number of
students scoring above 75%.

import pickle
def createfile():
fobj = open("student.dat","wb")
while True :
admno = int( input("Enter Admission Number : "))
name = input("Enter student Name : ")
per = float(input("Enter percentage : "))
rec = [admno , name , per]
pickle.dump(rec, fobj)
ch = input("Do you want to enter more(y/n) : ")
if ch not in ['y' , 'Y']:
break
fobj.close()
def countrec():
fobj = open("student.dat", "rb")
num = 0
try:
while True:
rec = pickle.load(fobj)
if rec[2] > 75:
num = num + 1
print(rec[0],rec[1],rec[2])
except:
fobj.close()
return num

def display():
with open("student.dat","rb") as f:
print("** Contents of file **")
print("Admno \t Name \t Percentage")
while True :
try:
R = pickle.load(f)
for x in R:
print(x , end = ' \t ')
print()
except :
break

createfile()
print()
display()
print()
n=countrec()
print("Total records with % > 75 = ", n)
Assuming the binary file is containing the following elements in the list:
1. Bus Number 2. Bus Starting Point 3. Bus Destination
a) Write a function create() in python to add records in the file.
b) Write a function in python to search and display details, whose destination is
“Cochin” from binary file “Bus.Dat”.

import pickle
def createfile():
fobj = open("bus.dat","wb")
while True :
bno = int( input("Enter Bus Number : "))
start = input("Enter starting point : ")
end = input("Enter destination point : ")
rec = [bno , start , end]
pickle.dump(rec, fobj)
ch = input("Do you want to enter more(y/n) : ")
if ch not in ['y' , 'Y']:
break
fobj.close()
def display():
with open("bus.dat","rb") as f:
print("** Bus Details with destination Cochin **")
print("BusNo \t Source \t Destination")
flag = 0
while True :
try:
R = pickle.load(f)
if R[2].lower() == 'cochin' :
flag=1
for x in R:
print(x , end = ' \t ')
print()
except :
break
if flag == 0 :
print("** No such record Found **")

createfile()
print()
display()
Write a function addrec() in Python to add more new records at the bottom of a
binary file “bus.dat”, assuming the binary file is containing the following
structure : [ bus no , source , destination]

import pickle
def addrec():
fobj = open("bus.dat","ab")
while True :
bno = int( input("Enter Bus Number : "))
start = input("Enter starting point : ")
end = input("Enter destination point : ")
rec = [bno , start , end]
pickle.dump(rec, fobj)
ch = input("Do you want to enter more(y/n) : ")
if ch not in ['y' , 'Y']:
break
fobj.close()
def display():
with open("bus.dat","rb") as f:
print("** Contents of file **")
print("BusNo \t Source \t Destination")
while True :
try:
R = pickle.load(f)
for x in R:
print(x , end = ' \t ')
print()
except :
break

addrec()
print()
display()
Structure of product contains the following elements [product code , product price]

a) Write a function create() to add records in the file.

b) Write a function searchprod(pc) in python to display the record of a particular product


from a file product.dat whose code is passed as an argument.

import pickle
def addrec():
fobj = open("product.dat","wb")
while True :
pno = int( input("Enter product code : "))
price = float(input("Enter product price : "))
rec = [pno , price]
pickle.dump(rec, fobj)
ch = input("Do you want to enter more(y/n) : ")
if ch not in ['y' , 'Y']:
break
fobj.close()
def display(c):
with open("product.dat","rb") as f:
print("** Contents of file **")
print("ProductNo \t Price")
flag = 0
while True :
try:
R = pickle.load(f)
if R[0] == c :
flag = 1
for x in R:
print(x , end = ' \t ')
print()
except :
break

addrec()
c = int(input("Enter code of product to search : "))
display(c)
MENU DRIVEN PROGRAM ON BINARY FILES TO PERFORM
THE FOLLOWING OPERATIONS

 INSERTION OF RECORDS

 DISPLAY RECORDS

 SEARCHING

 UPDATION

 DELETION
import pickle

def add() :
f=open("rec.txt",'ab')
while True:
print("\n")
admno = int(input("Enter admno : "))
name = input("Enter name : ")
clas = int(input("Enter class : "))
per = int(input("Enter percentage : "))

record = [admno , name , clas , per]


pickle.dump(record , f)

ch = input("Add more records? (Y/N) : ")


if ch=="n" or ch=="N":
break
f.close()
def display():
f = open('rec.txt','rb')
print("\n\n *** Student Records ***")
print("Admno \t Name \t Class \t Percentage")

try:
while True:
R = pickle.load(f)

for i in R:
print(i , end = ' \t')

print()
except:
print('')

f.close()
def search():
f = open('rec.txt','rb')
print("\n\n *** Records whose percentage is greater than 75 ***")
print("Admno \t Name \t Class \t Percentage")

try:
while True:
R=pickle.load(f)

if R[3] >= 75:


for i in R:
print(i , end = '\t')
print()

except:
print('')
def count():
f = open('rec.txt','rb')
cnt=0

try:
while True:
R=pickle.load(f)
for i in R[1]:
if i == 'a':
cnt += 1
except:
print('')

print("\n\n *** Count of a in name field ***")


print("Number of times a appears : ",cnt)
def modify():
f = open('rec.txt','rb+')
cnt=0
R = []
f.seek(0)

try:
i=0
while True:
x = pickle.load(f)
R.append(x)
R[i][3] = R[i][3]+3
i = i+1
except:
print('')
f.seek(0)
for rec in R:
pickle.dump(rec,f)
f.close()
print("*** Percentage of Records incremented by 3 ***")
display()
def delete():
f = open('rec.txt','rb')
cnt=0
R = []

try:
while True:
x = pickle.load(f)
if x[3]>=33 :
R.append(x)
except:
print('')

f.close()
f = open('rec.txt','wb')
for j in R:
pickle.dump(j,f)
f.close()

print("*** Records with percentage<33 Deleted ***")


display()
def menu():
while True:
print("\n\n ** Menu **")
print("1 : Add")
print("2 : Display all")
print("3 : Search")
print("4 : Count")
print("5 : Modify")
print("6 : Delete")
print("0 : Exit")

ch=int(input("Enter choice : "))


if ch==1:
add()
elif ch==2:
display()
elif ch==3 :
search()
elif ch==4 :
count()
elif ch==5 :
modify()
elif ch==6 :
delete()
elif ch==0 :
break

menu()
 Types of File Access

Sequential access. With this type of file access one must


read the data in order, much like with a tape, whether the
data is really stored on tape or not.

Random access (or direct access). This type of file access


lets you jump to any location in the file, then to any other,
etc., all in a reasonable amount of time.
FILE POINTERS

 Each file object has two integer values associated with it


:
 get pointer
 put pointer
 These values specify the byte number in the file where
reading or writing will take place.
File pointers…..

 By default get pointer is set at the beginning.

 By default put pointer is set at the beginning (when you


open file in write/append mode)

 By default put pointer is set at the end (when you add


record in file in append mode)
 There are times when you must take control of the file
pointers yourself so that you can read from and write to
an arbitrary location in the file.
Functions associated with file pointers :

 The seek() and tell() functions allow


you to set and examine the get
pointer/put pointer.
Syntax of tell()

This function gives the size in bytes i.e

Returns the current position of the getpointer or putpointer


from beginning of file (Number of bytes)
Return type is int datatype.

p = fp.tell()

fp is the file stream.


tell()
Syntax of seek()
These functions are used to move the record pointer from one
position to other position.
fp.seek(+/- n, position)

fp is the file stream.

+ means in forward direction


- means in backward direction
n means number of bytes

Position means 0 for beginning position


1 for current position
2 for end position
seek()
seek() function : (with one argument)

 With one argument :


fl.seek(k, 0)

where k is absolute position from the beginning.


The start of the file is byte 0
It will result in moving the pointer as shown-

File End
Begin

k bytes ^

File pointer
File Pointer calls

 fl.seek(0) Go to start

 fl.seek(0, 1) Stay at the current position

 fl.seek(0, 2) Go to the end of file

 fl.seek(m, 0) Move to mth byte in the file


File Pointer offset calls

 fl.seek(m, 1) Go forward by m bytes from current pos

 fl.seek(-m, 0) Remains at beginning

 fl.seek(-m, 2) Go backward by m bytes from the end

 fl.seek(0, 2) Remains at the end


 Backward movement of file pointer is not
possible from the beginning of the file.
 Forward movement of the file pointer is
not possible from the end of the file.
CSV
FILES
CSV FILES
 CSV files are the delimited files that store tabular data (data in
the form of rows and columns) where comma delimits every
value.
 Typically, the delimiter in a CSV file is a comma, but modern
implementation of CSV files allow other delimiter characters as
well.
 Each line in a CSV file is a data record.
 Each record consists of one or more fields, separated by comma.
CSV FILES
 Since CSV files are text files, we can apply text procedures on
them and then split the data retrieved using split(), but python
provides a better way to handle CSV files with the help of csv
module.

 Because it is a plain text file, it can contain only actual text data-
in other words printable ASCII or Unicode Characters
CSV FILES
The structure of a CSV file is given by its name.
Normally CSV files use a comma to separate each specific data value.

Here are some examples.


ID, NAME , FEE
12, Amar Saxena, 3000
15, Tarini Sen, 3200
Or
Rno, st_name, address
12, “ Amar Jain”, “59, ABC Nagar “
15, “ tarini Sen”, “45, Raj Nagar “
This CSV file contains 3 fields – Roll no, name and address which
are delimited by comma.
The problem is that the address field also contains comma.

There are three different ways to handle this situation:


 Use a different Delimiter- comma is default delimiter, which
can be changed to any other such as ‘\t’ or any other character. We
can do so by using the delimiter optional parameter.

 Wrap the data in Quotes- we can enclose the data in quotes, if it


includes the delimiter character. We can specify character for
quoting using quotechar optional parameter.

 Escape the delimiter character in the data –Escape characters


work in the same way as they work in format strings. If an escape
character is used, it must be specified using escapechar optional
parameter.
Why CSV files?
 Reading/Writing data in Text files is a common and simple
way to share information between programs. However,
CSV is the most popular format for exchanging data
nowadays as it can be directly used in text editors,
spreadsheets and databases.
 CSV files are popular because :
 They are easy to create
 Preferred export and import format for databases and
spreadsheets
 Capable of storing large amount of data
Python csv Module
 The csv module in python provides functionality to read and
write tabular data in CSV format.
 It provides two specific types of objects : reader and writer
objects – to read and write into csv files.
 The csv module’s reader and writer objects, read and write
delimited sequences as records in the csv file.
 Before we can use reader and writer objects of csv module in
our program, we have to import csv module in our program
with the help of following statement :
import csv
Opening and closing csv files
A csv file is opened and closed in the same manner as we open
and close text files.

Example of opening a csv file for writing


File = open (“data.csv” , “w”)

Example of opening a csv file for reading


File = open (“data.csv” , “r”)

Example of closing a csv file


File.close()
Writing in csv file
 Writing in csv files involves the conversion of user data into
writable delimited form and then storing it in the form of csv
file.
For writing in a csv file, we use the following 3 functions :

csv.writer() Returns a writer object which writes data in a csv file

writerow() Writes one row of data onto the writer object


Writes multiple rows of data onto the writer object.
It writes data as a nested sequence.
writerows() Example:
records = [ [1,’aman’,98] , [2,’rohan’,87] , [3,’sia’,99] ]
obj.writerows(records)
To write Data into csv file
csv.writer() : Required to write data into file when opened
in “w” or “a” mode

Syntax:
with open(“ filename”, “mode” ) as csv_object:
writer_object=csv.writer(csv_object,delimiter=‘,’)
writer_object=csv.writerow(list content)

Note: delimiter is optional


Need of writer object

 Since csv files are flat files, before writing onto them, the data
must be in csv-writable delimited form, due to this we need to
convert data received from the user in a form which is
appropriate for csv files. This task is performed by writer
object.
 The data row written onto the writer object (using writerow()
and writerows() ) gets converted to csv writable form and
then is written onto the linked csv file.
STEPS TO WRITE INTO A CSV FILE
1. Import csv module
2. Open csv file similar to how we open text files
File = open (“data.csv” , “w”) (Extension of a csv file is .csv)
3. Create writer object with the following statement:
obj = csv.writer (File)
In the above statement, DELIMITER ARGUMENT IS NOT SPECIFIED. When we
skip the delimiter argument, python considers the default delimiter argument for
csv files, i.e., comma. But if we want to specify a delimiter character explicitly,
then it is done as below:
obj = csv.writer (File , delimiter = ‘|’)
(Here, delimiter is explicitly specified as pipe symbol (|) )
4. Obtain user data and create python sequence with it.
record = [11, ‘aman’ , 95]
5. Write the python sequence (list in this case) onto the writer object using
writerow() or writerows() functions.
obj.writerow(record)
6. Close the file
File.close()
TO SEND ONE RECORD IN CSV FILE

import csv
with open('data1.csv','w') as cf:
cv = csv.writer (cf, delimiter=',')
cv.writerow(['Team','Score'])
cv.writerow(['A',89])
cv.writerow(['B',79])
cv.writerow(['C',91])
print("** File Created **")
TO SEND MULTIPLE RECORDS IN CSV FILE
(one by one)
import csv
with open('data1.csv','w') as cf:
cv=csv.writer(cf, delimiter=',')
while True:
ino = int(input("Enter item Number : "))
iname = input("Enter item Name : ")
qty = int(input("Enter quantity : "))
rec=[ino,iname,qty]
cv.writerow(rec)
ch=input("Enter More recordS? (Y/N) : ")
if ch in ['n' , 'N']:
break
print("** File created **")
TO SEND MULTIPLE RECORDS IN CSV FILE
(all in one go)
import csv
with open('data1.csv','w') as cf:
cv=csv.writer(cf, delimiter=',')
rec = []
while True:
ino = int(input("Enter item Number : "))
iname = input("Enter item Name : ")
qty = int(input("Enter quantity : "))
rec.append([ino,iname,qty])

ch=input("Enter More recordS? (Y/N) : ")


if ch in ['n' , 'N']:
break
cv.writerows(rec)
print("** File created **")
Reading in csv file

 Reading from a csv file involves loading of csv file’s data,


parsing it i.e. removing its delimitation, loading it in a python
iterable (eg : strings, tuples, lists) and then reading from this
iterable.
 To read from a csv file, we use the following function:
csv.reader() : It returns a reader object which loads
data from csv file into an iterable after parsing the
delimited data.

 csv.reader() does the opposite of csv.writer()


To read information from csv file
csv.reader() : This method is used to read data
from csv file when opened in “r” mode.
Syntax:

with open(“ filename”, “mode” ) as csv_object:


reader_object=csv.reader(csv_object,delimiter=‘,’)

Note: delimiter is optional


STEPS TO READ FROM A CSV FILE
1. Import csv module
2. Open csv file similar to how we open text files
File = open (“data.csv” , “r”) (Extension of a csv file is .csv)
3. Create reader object with the following statement:
obj = csv.reader (File)
In the above statement, DELIMITER ARGUMENT IS NOT SPECIFIED.
When we skip the delimiter argument, python considers the default
delimiter argument for csv files, i.e., comma. But if we want to specify a
delimiter character explicitly, then it is done as below:
obj = csv.writer (File , delimiter = ‘|’)
(Here, delimiter is explicitly specified as pipe symbol (|) )
4. The reader object stores the parsed data in the form of iterable and now
we can traverse the iterable and print it.
5. Close the file
File.close()
Role of argument newline in opening of csv files

 While opening csv files, in the open(), we can specify an additional argument
newline, which is an optional argument.
 The role of newline argument is to specify how would python handle newline
characters while working with csv files.
 As csv files are text files, EOL character is inserted in the end of each line in csv
files. Different operating systems store EOL characters differently. The EOL on one
operating system will not be treated as EOL on another operating system. To
handle this situation, we can suppress the EOL translation by specifying third
argument of open() as newline=’‘ (null string – no space in between the quotes).
 If we specify a newline argument while writing into a csv file, it will create a csv file
with no EOL translations and we will be able to use csv file in normal way on any
platform,
 We can use newline argument as below:
File = open(“data.csv” , ‘w’ , newline=’’)
To read records from csv file

import csv
with open('data1.csv','r') as cf:
cv=csv.reader(cf, delimiter=',')
print(next(cv))
print(next(cv))

Method 1 : when we
know the no. of records
in csv file
To read records from csv file

import csv
with open('data1.csv','r') as cf:
cv=csv.reader(cf, delimiter=',')
for c in cv :
print(c)

Method 2 : when we do
not know the no. of
records in csv file
To read records from csv file

import csv
try:
with open('data1.csv','r') as cf:
cv=csv.reader(cf, delimiter=',')
for c in cv:
print(c )
except FileNotFoundError:
print('File not found') Method 3 : using try
except block
To search for a specific record in csv file
import csv
try:
flag = 0
with open('data1.csv','r') as cf:
cv=csv.reader(cf, delimiter=',')
x = int(input("Enter item no. to search : "))
for c in cv:
if int(c[0]) == x : Records are saved in
flag = 1
csv file in the form of
print(c )
except FileNotFoundError: string, so it needs to
print('File not found') be type casted before
comparision
if flag==0 :
print("** Record Not Found **")
MENU DRIVEN PROGRAM ON CSV FILES TO PERFORM
THE FOLLOWING OPERATIONS

 INSERTION OF RECORDS

 DISPLAY RECORDS

 SEARCHING
import csv

def add() :
f = open("data.csv",'w',newline='')
cv = csv.writer(f , delimiter=',')
while True:
print("\n")
admno = int(input("Enter admno : "))
name = input("Enter name : ")
clas = int(input("Enter class : "))
per = int(input("Enter percentage : "))

record = [admno , name , clas , per]


cv.writerow(record)

ch = input("Add more records? (Y/N) : ")


if ch=="n" or ch=="N":
break
f.close()
def display():
f = open('data.csv','r')
cv = csv.reader(f , delimiter=',')
print("\n\n *** Student Records ***")
print("Admno \t Name \t Class \t Percentage")
for c in cv :
for data in c:
print(data , end = '\t')
print()
f.close()
def search():
f = open('data.csv','r')
cv = csv.reader(f , delimiter=',')

print("\n\n *** Student Records with % > 75***")


print("Admno \t Name \t Class \t Percentage")
for c in cv :
if int(c[3]) > 75 :
for data in c:
print(data , end = '\t')
print()
def count():
f = open('data.csv','r')
cv = csv.reader(f , delimiter=',')
cnt=0
for c in cv :
for char in c[1]:
if char == 'a':
cnt += 1
print("\n\n *** Count of a in name field ***")
print("Number of times 'a' appears : ",cnt)
def menu():
while True:
print("\n\n ** Menu **")
print("1 : Add")
print("2 : Display all")
print("3 : Search")
print("4 : Count")
print("0 : Exit")

ch=int(input("Enter choice : "))


if ch==1:
add()
elif ch==2:
display()
elif ch==3 :
search()
elif ch==4 :
count()
elif ch==0 :
break

menu()
Functions under os module
import os
1. getcwd() to know the name of the current working
directory
str=os.getcwd()
2. abspath() returns complete path name of data file.
os.path.abspath(filename)
3. rename() used to rename a file
os.rename(oldfile_name, new file_name)
4. remove() to delete an existing file
os.remove(file_name)
5. truncate(n) Resizes the file to n bytes
file_object.truncate(5)

You might also like