Practical - File - Class - XII - CS - 2024 For Batch 2024 205
Practical - File - Class - XII - CS - 2024 For Batch 2024 205
def simple_int(p,t,r=6):
return (p*r*t)/100
t=int(input('Enter Time'))
if t<=1:
p=int(input('Enter principal amount'))
print('Rate of interest taken by deafult 6%')
si=simple_int(p,t)
elif t>1 and t<=5:
p=int(input('Enter principal'))
r=7
print('Rate of interest is',r)
si=simple_int(p,t,r)
else:
p=int(input('Enter principal'))
r=8
print('Rate of interest is',r)
si=simple_int(p,t,r)
print('Simple interest is', si)
def sample(x):
for i in range(0,len(x)):
if i%2!=0:
x[i]=x[i]+5
print('List inside function',x)
l=[10,20,30,40,50]
print('Original list',l)
sample(l)
print('List after function use',l)
def createdict(x):
ch='y'
while(ch!='n'):
r=int(input('Enter rollno'))
n=input('Enter namr')
m=int(input('Enter marks'))
dob=input('Enter dob')
l=[n,m,dob]
x[r]=l
print('Do you want to create more elements')
ch=input('Enter choice y/n')for i in x:
def display():
if x[i][1]>=300:
print('Roll no :',i)
print('Name\t\t dob \t\t marks')
l=x[i]
for j in l:
print(j,end='\t\t')
print()
d={}
createdict(d)
display(d)
4. Write a function which twice the number if it is odd elements of list
and square if it is even element of list.
def twicesquare(x):
l=len(x)
for i in range(0,l):
if x[i]%2==0:
x[i]=x[i]**2
else:
x[i]=2*x[i]
list=[]
n=int(input('Enter how many elements'))
for x in range(1,n+1):
d=int(input('Enter element'))
list.append(d)
print(list)
twicesquare(list)
print('final list is')
print(list)
def createfile():
f=open("myfile.txt", "w")
f.write("We are in class 12th \n")
f.write("We are studying Python Programming \n")
f.write("Python is a high level language \n")
print("Data written to the file successfully")
f.close()
def readfile():
f= open("myfile.txt","r")
lines=f.readlines()
for x in lines:
print(x)
f.close()
def appendfile():
f=open("myfile.txt", "a")
while True:
s=input('Enter line of text')
f.write(s+'\n')
ch=input('More line of textyes/no')
if ch in ('no','NO','No'):
break
while True:
print("press 1 to create file")
print("press 2 to read file")
print("press 3 to Append file")
print("Enter your choice")
ch=int(input())
if ch==1:
createfile()
elif ch==2:
readfile()
elif ch==3:
appendfile()
else:
break
9. Write a program which counts no of consonent, no of words and no of
words containing digits in a text file.
f= open("c:/users/hp/onedrive/documents/myfile1.txt")
data=f.read()
c=False
cnn=0
for x in data:
c=x not in 'AEIOUaeiou'
if c==True:
cnn=cnn+1
print('No. of consonent',cnn)
list=data.split(' ')
print(list)
w=0
cn=0
c=False
for x in list:
print(x)
w=w+1
c=False
for i in x:
c= i in '0123456789'
if c==True:
break
if c==True:
cn=cn+1
print(data)
print("Number of Words containg digits",cn)
print("Number of words",w)
f.close()
10. Write a program which counts no of lines starting with A in a text file
f=open("C:/Users/hp/Documents/myfile.txt","r")
'''c=0
while True:
l=f.readline()
if l=='':
break
if l[0]=='A':
print(l)
c=c+1
c=1
for l in list:
if l[0]=='A':
print(l,end='')
c=c+1
print('No. of lines starting with A are:',c)
print(list)'''
x=f.readlines()
print(x)
f.close()
11. Program to implement binary file operation such as
1. create binary ("student.dat") which store students records as
rollno, name, class & marks
2. read file and display it content
3. Append file - add record at end of file
4. Modify record- Modify particular record
5. Search record- search particular record
import pickle
def createfile():
f=open('student.dat','wb')
data=[]
ch='Y'
while ch=='Y' or ch=='y':
rno=int(input('Enter rollno'))
name=input('Enter name')
cl=input('Enter class')
marks=int(input('Enter namrks'))
x=[rno,name,cl,marks]
data.append(x)
print('Enter more records Y/N')
ch=input()
pickle.dump(data,f)
print('Data successfully saved')
def readfile():
f=open('student.dat','rb')
try:
while True:
data=pickle.load(f)
for rec in data:
print('Rollno: ',rec[0])
print('Name: ',rec[1])
print('Class : ',rec[2])
print('marks :',rec[3])
print()
except Exception:
f.close()
def searchrec():
r=int(input('Enter roll no of student to be searched'))
f=open('student.dat','rb')
found=False
try:
while True:
data=pickle.load(f)
for rec in data:
if r==rec[0]:
found=True
print('Name: ',rec[1])
print('Class : ',rec[2])
print('marks :',rec[3])
break
except Exception:
f.close()
if found==True:
print('Search successful')
else:
print('Record not exist')
def modifyrec():
f=open('student.dat','rb+')
r=int(input('Enter rollno of student for correction'))
found=False
f.seek(0)
try:
while True:
pos=f.tell()
data=pickle.load(f)
print(pos)
for rec in data:
if r==rec[0]:
found=True
rec[1]=input('Enter Name')
rec[2]=input('Enter Class')
rec[3]=int(input('Enter marks'))
f.seek(pos)
pickle.dump(data,f)
break
except Exception:
f.close()
print(data)
def createfile():
f=open("myfile.txt","w")
while True:
s=input("Input String")
f.write(s+'\n')
c=input("More string y/n")
if c=='n':
break
f.close()
def readfile():
f=open("myfile.txt","r")
print("\nFile Postion",f.tell())
s=f.readlines()
print(s)
for x in s:
print(x)
f.close()
def countlines():
f=open("myfile.txt","r")
data=f.readlines()
print(data)
c=0
for x in data:
for j in x:
if j[0]=='I':
c=c+1
print('no of lines start with A',c)
f.close()
def appendfile():
f=open("myfile.txt","a")
while True:
s=input("Input String")
f.write(s+'\n')
c=input("More string y/n")
if c=='n':
break
f.close()
def modify():
f=open("myfile.txt","r+")
s=f.readline()
i=1
while s!='':
line=''
s=s.rstrip()
pos=f.tell()
print("\n print position after reading",i,"string ",pos)
L=len(s)
print("Length of",i,"string",L)
line=line+s[0].upper()+s[1:]
print(line)
f.seek(pos-(L+2))
f.write(line+"\n")
print(f.tell())
f.seek(f.tell())
s=f.readline()
i=i+1
f.close()
def countwords():
f=open("myfile.txt","r")
s=f.read()
l=s.split(' ')
print(l)
count=0
for x in l:
if x=='me' or x=='my':
count=count+1
print(count)
while True:
print("\n1- create file")
print("\n2-readfile")
print("\n3- append ")
print("\n4-modify")
print("\n5- count words")
print("\n6 - count lines start with A")
ch=input("\n Input Choice")
if ch=='1':
createfile()
elif ch=='2':
readfile()
elif ch=='3':
appendfile()
elif ch=='4':
modify()
elif ch=='5':
countwords()
elif ch=='6':
countlines()
else:
break;
13. Write a program to perform text file operations as:
1. create file
2. reead file
3. count occurrences of word ‘The’ or ‘the in a file
4. count words which have digits in it.
def createfile():
f=open('myfile.txt','w')
while True:
s=input('Enter Text')
f.write(s+'\n')
print("More text y/n")
c=input()
if c in 'Nn':
break
f.close()
def occurrence():
f=open('myfile.txt','r')
data=f.read()
s=list(data.split(' '))
c=0
for x in s:
if x in ('The','the'):
c=c+1
print('Ocuurence of The is',c)
f.close()
def countwords():
f=open("myfile.txt","r")
s=f.read()
l=s.split()
print(l)
count=0
for x in l:
print (x.isalnum())
if not x.isalpha():
count=count+1
print("no of words",count)
print("1- cratefile")
print("2- occurrence")
print("3-count words")
print("Enter Choice")
ch=input()
if ch=='1':
createfile()
elif ch=='2':
occurrence()
elif ch=='3':
countwords()
else:
print("Wrong choice")
def readfile():
f=open('student.dat','rb')
try:
while True:
data=pickle.load(f)
for rec in data:
print('Rollno: ',rec[0])
print('Name: ',rec[1])
print('Class : ',rec[2])
print('marks :',rec[3])
print()
except Exception:
f.close()
def searchrec():
r=int(input('Enter roll no of student to be searched'))
f=open('student.dat','rb')
found=False
try:
while True:
data=pickle.load(f)
for rec in data:
if r==rec[0]:
found=True
print('Name: ',rec[1])
print('Class : ',rec[2])
print('marks :',rec[3])
break
except Exception:
f.close()
if found==True:
print('Search successful')
else:
print('Record not exist')
def modifyrec():
f=open('student.dat','rb+')
r=int(input('Enter rollno of student for correction'))
found=False
f.seek(0)
try:
while True:
pos=f.tell()
data=pickle.load(f)
print(pos)
for rec in data:
if r==rec[0]:
found=True
rec[1]=input('Enter Name')
rec[2]=input('Enter Class')
rec[3]=int(input('Enter marks'))
f.seek(pos)
pickle.dump(data,f)
break
except Exception:
f.close()
print(data)
while True:
print('File Operation on Student File')
print('1-create file')
print('2- readfile')
print('3= search a record')
print('4- Modifiy record')
ch=int(input('Enter choice'))
if ch==1:
createfile()
elif ch==2:
readfile()
elif ch==3:
searchrec()
elif ch==4:
modifyrec()
else:
print('Exiting')
break
15. Program to connect with database and store records of students.
import mysql.connector
mydb=mysql.connector.connect(host='localhost',user='root',passwd='root',dat
abase='school')
mycursor=mydb.cursor()
n=int(input("ENTER HOW MANY REDORDS"))
i=1
rq="insert into student (rno, name, dob) values (%s,%s,%s)"
list=[]
while i <= n:
r=int(input('enter roll no'))
nm=input('enter name')
d=input('enter date of birth')
rtup=(r,nm,d)
list.append(rtup)
i=i+1
print(list)
mycursor.executemany(rq,list)
mydb.commit()
print(mycursor.rowcount,"Record saved")
16. Program to connect with database and search student based on rollno in
table student and display record, if rollno not found display appropriate
message.
import mysql.connector
mydb=mysql.connector.connect(host='localhost',user='root',passwd='root',dat
abase='school')
mycursor=mydb.cursor()
r= int(input('roll no'))
t=(r,)
mycursor.execute("select * from student where rno=%s",t)
for x in mycursor:
print(x)
17. Program to connect with database and update the student record of
entered rollno.
import mysql.connector
mydb=mysql.connector.connect(host='localhost',user='root',passwd='root',dat
abase='school')
mycursor=mydb.cursor()
mycursor.execute("update 12a set name='sss' where rno=5")
print("record updated")
mydb.commit()
The
M001 Kashmir Action 2022/01/26 1245000 1300000
Files
Looop
M003 Thriller 2022/02/01 250000 300000
Lapeta
Shabaash Biograp
M005 2022/02/04 1000000 800000
Mithu hy
Romanc
M006 Gehraiyaan 2022/02/11 150000 120000
e
Answers:
Output:
2. select distinct from a movie;
Answers:
[1] select pow(5,3);
SecondTea
MatchID MatchDate FirstTeamID SecondTeamID FirstTeamScore
mScore
M1 2021/12/20 1 2 107 93
M3 2021/12/22 1 3 86 81
M4 2021/12/23 2 4 65 67
M5 2021/12/24 1 4 52 88
M6 2021/12/25 2 3 97 68
Answers:
desc team;
Inserting data:
-> values(1,'Tehlka');
Show the content of table – team:
Answers: