Professional Documents
Culture Documents
Data
PROGRAM
(Records to (records
be to be
extracted) inserted)
DEVICES OR
FILES
Data
Types of Files
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
object_name = open(filename)
OR
object_name = open(filename, mode)
OR
with open(“filename”,”mode”) as object_name:
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
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
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=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
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
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.
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.
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
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
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
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
for x in words:
if x == ‘me’ :
count += 1
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
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)
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
Method – 2
iff=open('text.txt','r')
count1=count2=count3=0
lines = iff.readlines()
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:
________________________________________________________________
________________________________________________________________
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)
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)
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")
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
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
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
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" }
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)
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)
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 : "))
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 : "))
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
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
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)
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]
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 : "))
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)
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('')
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()
menu()
Types of File Access
p = fp.tell()
File End
Begin
k bytes ^
File pointer
File Pointer calls
fl.seek(0) Go to start
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.
Syntax:
with open(“ filename”, “mode” ) as csv_object:
writer_object=csv.writer(csv_object,delimiter=‘,’)
writer_object=csv.writerow(list content)
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])
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 : "))
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)