Text Files and Binary Files (Lab Programs)
Text Files and Binary Files (Lab Programs)
f= open("story.txt","r")
contents=f.readlines()
for line in contents:
words=line.split()
for i in words:
print(i+ "#",end=" ")
f.close()
2. Creating a python program to read a text file and display the number of
vowels/consonants/lowercase/uppercase.
“To write a python program to read a text file “story.txt” and displays the number of
vowels, consonants,lowercase,uppercase”.
f=open("story.txt","r")
contents=f.read()
vowels=0
consonants=0
lowercase=0
uppercase=0
for ch in contents:
if ch in 'aeiouAEIOU':
vowels=vowels+1
if ch in 'bcdfgjklmnpqrstvwxyzBCDFGJKLMNPQRSTVWXYZ':
consonants=consonants+1
if ch.islower():
lowercase=lowercase+1
if ch.isupper():
uppercase=uppercase+1
f.close()
print("The total number of vowels in file :",vowels)
print("the total number of consonants in file:",consonants)
print("the total number of lowercase letters in file:",lowercase)
print("the total number of uppercase letters in file:",uppercase)
3. Creating a python program to copy particular lines from one text file to another.
“To write a python program to read lines from a text file “sample.txt” and copy those
lines into another which are starting with ‘a’ or ‘A’ “
f1=open(“sample.txt”,”r”)
f2=open(“new.txt”, “w”)
while True:
line=f1.readline()
if line== ’ ‘ :
break
if line[0]==’a’ or line[0]==’A’:
f2.write(line)
print(“All lines which are starting with ‘a’ or ‘A’ has been copied successfully into a
new file”)
f1.close()
f2.close()
Binary files
1. Creating a python program to create and search records in a binary files.
“To write a python program to create a binary file with roll no, name and
marks. Search for a given roll number and display the name and marks , if
not found display appropriate message”
import pickle
def create():
f=open("student.dat","ab")
opt='y'
while opt=='y':
print("enter the details")
rollno=int(input("enter the roll number"))
name=input("enter the name")
marks=int(input("enter the marks"))
L=[rollno,name,marks]
pickle.dump(L,f)
opt=input("do you want to add another record(y/n)?:")
f.close()
def search():
f=open("student.dat","rb")
no=int(input("enter the roll number of a student to search"))
found=0
try:
while True:
s=pickle.load(f)
if s[0]==no:
print("the searched roll no is found and details are:",s)
found=1
break
except:
f.close()
if found==0:
print("the searched roll no is not found")
#main program
create()
search()
2. Creating a python program to create and update records in a binary files.
“To write a python program to create a binary file with roll no, name, age and
marks. Update the marks given for the roll no”.
import pickle
def create():
f=open("student.dat","ab")
opt='y'
while opt=='y':
print("enter the details")
rollno=int(input("enter the roll number"))
name=input("enter the name")
age=int(input(“enter the age “))
marks=int(input("enter the marks"))
L=[rollno,name,age,marks]
pickle.dump(L,f)
opt=input("do you want to add another record(y/n)?:")
f.close()
def update():
f=open("student.dat","rb+")
no=int(input("enter the roll number of a student to update"))
found=0
try:
while True:
pos=f.tell()
s=pickle.load(f)
if s[0]==no:
print("the searched roll no is found and details are:",s)
m=int(input("enter the new marks of the student"))
s[3]=m
f.seek(pos)
pickle.dump(s,f)
found=1
print("mark updated successfully and details are",s)
break
except:
f.close()
#main program
while True:
n=int(input("1. enter the students details\n 2. update the marks\n 3. exit\n enter your
choice"))
if n==1:
create()
elif n==2:
update()
else:
break
3. Creating a python program to create and update records in a binary files
using dictionary.
“To write a python program to create a binary file with roll no, name, age and
marks. Update the marks given for the roll no using dictionary”.
import pickle
def create():
f=open("info.bin","ab+")
dic={}
n=int(input("enter the number of students"))
for key in range(n):
rollno=int(input("enter the roll number"))
name=input("enter the name")
marks=int(input("enter the marks"))
dic[rollno]={ }
dic[rollno]["name"]=name
dic[rollno]["marks"]=marks
pickle.dump(dic,f)
f.close()
def update():
f=open("info.bin","rb+")
d=pickle.load(f)
rollno=int(input("enter the roll number of the student to update"))
for key in d:
if key==rollno:
m=int(input("enter the new marks of the student"))
d[key]["marks"]=m
print(d)
if rollno not in d:
print("roll no doesnot exist")
break
pickle.dump(d,f)
f.close()
#main program
while True:
n=int(input("1.enter details \n 2. update the details \n 3. exit\n enter your choice:"))
if n==1:
create()
elif n==2:
update()
else:
break