0% found this document useful (0 votes)
17 views6 pages

Xii Pract prg6 - 11

The document contains the code for 10 different Python programs dealing with text files and binary files. The programs demonstrate how to read and write to text files, count characters, add records, and search binary files. They provide examples of opening, reading, writing, appending and closing files, as well as using while loops and taking user input.

Uploaded by

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

Xii Pract prg6 - 11

The document contains the code for 10 different Python programs dealing with text files and binary files. The programs demonstrate how to read and write to text files, count characters, add records, and search binary files. They provide examples of opening, reading, writing, appending and closing files, as well as using while loops and taking user input.

Uploaded by

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

CLASS – XII COMPUTER SCIENCE RECORD

PROGRAM : 6 DATE :
__________________

TEXT FILE - READ

QUES: Write a program to read a text file line by line and display each word
separated by a #.

AIM: To write a python program to to read a text file line by line and display each
word separated by a #.

CODE:
def Display():
f=open(r"d:/test.txt","r")
lines=f.readlines()
for i in lines:
word=i.split()
for i in word:
print(i,end='#')
print()
f.close()

while True:
print("1.Display word with #\n2. Exit")
ch=int(input("Enter a choice"))
if ch==1:
Display()
elif ch==2:
break
else:
print("Invalid input....Try again...")

PROGRAM : 7 DATE :
__________________
TEXT FILE - COUNT
QUES: Write a menu driven program to read a text file and perform the following
operations:
1. Display the number of vowels and consonants in the file.
2. Display the number of lowercase and uppercase in the file
3. Exit.
AIM: To write a menu driven program to read a text file and perform the given
operations
CODE:
def VowCons():
f=open("D:\\test.txt","r")
d=f.read()
v=c=0
for i in d:
if i.isalpha():
if i in ‘aeiouAEIOU’
v+=1
else:
c+=1
print("Number of vowels are ",v)
print("Number of consonants are ",c)
f.close()

def LowUpp():
f=open("D:\\test.txt","r")
d=f.read()
lc=uc=0
for i in d:
if i.isalpha():
if i.islower():
lc+=1
else:
uc+=1
print("Number of lowercase characters are ",lc)
print("Number of uppercase characters are ",uc)
f.close()

while True:
print(''1. No. of vowels and consonants\n2. No. of lower and upper case\n3. No. of
the
word\n4. Exit'')
ch=int(input("Enter your choice:"))
if ch==1:
VowCons()
elif ch==2:
LowUpp()
elif ch==3:
break
else:
print("Invalid entry....Try again....")

PROGRAM : 8 DATE :
__________________

TEXT FILE – WRITE

QUES: Write a python program to get roll number, names and marks of 3 students of
a class (get from user) and store the details in a file named ‘marks.txt’
AIM: To get roll number, names and marks of 3 students of a class (get from user)
and store the details in a file named ‘marks.txt’

CODE:
def Write():
f=open(r"d:/marks.txt","w")
for i in range(3):
rno=input("enter roll no : ")
name=input("enter name : ")
marks=input("enter marks : ")
f.write(rno+','+name+','+marks+'\n')
f.close()

def Display():
f=open(r"d:/marks.txt","r")
print(f.read())

while True:
print("1.Write\n2. Display\n3.Exit")
ch=int(input("Enter a choice"))
if ch==1:
Write()
elif ch==2:
Display()
elif ch==3:
break
else:
print("Invalid input....Try again...")

PROGRAM : 9 DATE :
__________________

TEXT FILE – APPEND

QUES: Write a python program to add two more student records with roll number,
names and marks into the file named ‘marks.txt’

AIM: To add two more student records with roll number, names and marks into the
file named ‘marks.txt’

CODE:
def Append():
f=open(r"d:/marks.txt","a")
for i in range(2):
rno=input("enter roll no : ")
name=input("enter name : ")
marks=input("enter marks : ")
f.write(rno+','+name+','+marks+'\n')
f.close()

def Display():
f=open(r"d:/marks.txt","r")
print(f.read())

while True:
print("1.Append\n2. Display\n3.Exit")
ch=int(input("Enter a choice"))
if ch==1:
Append()
elif ch==2:
Display()
elif ch==3:
break
else:
print("Invalid input....Try again...")

PROGRAM : 10 DATE :
__________________
BINARY FILE – READ AND WRITE
QUES: Write a python program to get admission number, name and total marks of 3
students from user and store the details in a binary file named ‘stud.dat’. Also read
and display the file.

AIM: To get admission number, name and total marks of 3 students from user and
store the details in a binary file named ‘stud.dat’. Also to read and display the file.

CODE:
import pickle

def Add():
d={}
f=open("stud.dat","wb")
for i in range(3):
admno=int(input("Enter Admission no:"))
name=input("Enter student name:")
tot_marks=int(input("Enter Total marks obtained:"))
d['Admno']=admno
d['Name']=name
d['Tot Marks']=tot_marks
pickle.dump(d,f)
print("Record",i+1," Added Successfully.")
f.close()

def Display():
f=open("stud.dat","rb")
while True:
try:
d=pickle.load(f)
print(d)
except EOFError:
break
f.close()

while True:
print("1. Add Record\n2. Display Record\n3. Exit")
ch=int(input("Enter your choice:"))
if ch==1:
Add()
elif ch==2:
Display()
elif ch==3:
break
else:
print("Invalid entry....Try again....")

PROGRAM : 11 DATE :
__________________

BINARY FILE –SEARCH

QUES: Write a python program to search a binary file “stud.dat” and display the
records whose total marks is greater than 400.

AIM: To search a binary file “stud.dat” and display the records whose total marks is
greater than 400.

CODE:
import pickle

def Search():
f=open("stud.dat","rb")
found=False
while True:
try:
d=pickle.load(f)
if d['Total Marks']>400:
print(d)
found=True
except EOFError:
break
if found==False:
print("Record not found...")
f.close()

while True:
print("1. Search\n2.Exit")
ch=int(input("Enter your choice:"))
if ch==1:
Search()
elif ch==2:
break
else:
print("Invalid entry....Try again....")

You might also like