lab record
lab record
12
AIM: To write a python program to create a file
with the name of the file & content of the file line
by line & display each word separated by #.
PROGRAM:
def create_file(name):
myfile = open(name, "w")
while True:
data = input("Enter a sentence: ")
myfile.write(data + '\n')
L = input("Do you want to enter more lines?
(y/n): ")
if L == 'n' or L == 'N':
break
myfile.close()
def display_file(name):
myfile = open(name, "r")
for i in myfile:
words = i.split()
for x in words:
print(x + '#', end=" ")
print()
myfile.close()
RESULT:
Program was executed successfully and output
obtained
OUTPUT:
Enter the name of the file to be created: data
Enter a sentence: program was executed
successfully
Do you want to enter more lines? (y/n): n
program# was# executed# successfully#
#Experiment No. 13
AIM: To write a python program to read a text file
and display the number of vowels, consonants,
uppercase & lowercase characters in the file.
PROGRAM:
myfile = open("sample.txt", "r")
ch = myfile.readlines()
vcount = ccount = upcount = lowcount = 0
for line in ch:
print(line)
for word in line:
if word.isalpha() == True:
if word in "aeiouAEIOU":
vcount += 1
else:
ccount += 1
for line in ch:
for word in line:
if word.isupper() == True:
upcount += 1
elif word.islower() == True:
lowcount += 1
print("vowels:", vcount)
print("consonants:", ccount)
print("uppercase:", upcount)
print("lowercase:", lowcount)
myfile.close()
RESULT:
Program was executed successfully and output
obtained
OUTPUT:
Hi Hello
How are you
vowels: 8
consonants: 8
uppercase: 3
lowercase: 13
#Experiment No. 14
AIM: To write a python program to remove all the
lines that contain the character ‘a’ in a text file &
write it into another file.
PROGRAM:
file1=open("main.txt", "r")
file2=open("new_file.txt", "w")
c=file1.readlines()
for line in c:
if 'a' not in line:
file2.write(line)
file1.close()
file2.close()
file2=open("new_file.txt", "r")
print("Content of the file after removing line with
character 'a'")
for i in file2:
print(i)
file2.close()
RESULT:
Program was executed successfully and output
obtained
OUTPUT:
I am main
how are you?
I'm fine
RESULT:
Program was executed successfully and output
obtained
OUTPUT:
**Menu**
1. Add records
2. Search for a record
3. Exit
Enter your choice: 1
Enter roll number: 5
Enter name: diya
Do you want to add more records? (y/n): n
**Menu**
1. Add records
2. Search for a record
3. Exit
Enter your choice: 2
Enter the roll number to search: 5
Name of student is; diya
**Menu**
1. Add records
2. Search for a record
3. Exit
#Experiment No. 16
AIM: To write a python program to create a
binary file which contains the student details &
provide options to update the student details
based on the roll number entered by the user.
PROGRAM:
import pickle
def addrec():
a=open("student.dat","wb")
print("add student details:")
l=[]
ans='y'
while ans=='y':
r=int(input("enter the roll numbr:"))
n=input("enter name:")
m=float(input("enter marks:"))
l.append([r,n,m])
ans=input("Do you want to add another record?
(y/n)")
pickle.dump(l,a)
print("Record stored successfully!")
def updaterec():
a=open("student.dat", "rb")
r=int(input("enter the roll number which you want to
update:"))
b=pickle.load(a)
l=[]
for i in b:
if r==i[0]:
nm=float(input("enter new mark:"))
i[2]=nm
break
def deleterec():
a=open("student.dat", "rb")
r=int(input("enter the roll number which you want to
be deleted:"))
b=pickle.load(a)
l=[]
for i in b:
l.append(i)
for i in l:
if r==i[0]:
l.remove(i)
break
a.close()
a=open("student.dat", "wb")
pickle.dump(l,a)
print("student detail deleted successfully!")
a.close()
def displayrec():
a=open("student.dat","rb")
r=int(input("enter the roll number to display:"))
b=pickle.load(a)
for i in b:
if r==i[0]:
print("roll.no:",i[0],"\nNmae:",i[1],"\nMark:",i[2])
break
else:
print("student details not found!")
print("**menu**")
print("1.Add student:\n2. Update student details:\n3.
Delete students details:\n4. Display student details:")
c=int(input("enter your choice:"))
if c==1:
addrec()
elif c==2:
updaterec()
elif c==3:
deleterec()
elif c==4:
displayrec()
else:
print("Invaid choice")
RESULT:
Program was executed successfully and output
obtained
OUTPUT:
**menu**
1.Add student:
2. Update student details:
3. Delete students details:
4. Display student details:
enter your choice:1
add student details:
enter the roll numbr:1
enter name:devika
enter marks:75
Do you want to add another record?(y/n)y
enter the roll numbr:2
enter name:kashi
enter marks:85
Do you want to add another record?(y/n)y
enter the roll numbr:3
enter name:dev
enter marks:80
Do you want to add another record?(y/n)n
Record stored successfully!