FILE ORGANISATION ASSIGNMENT-2
CODES...
# To insert data in form of records...
import pickle
def insert():
a=[]
while True:
r=int(input("Enter roll no. : "))
n=input("Enter name : ")
m=float(input("Enter marks : "))
l=[r,n,m]
a.append(l)
q=input("Do you want to enter more records? (y/n) : ")
if q=='n' or q=='N':
break
f=open("project.dat",'wb')
pickle.dump(a,f)
print("Data inserted in form of Records")
f.close()
insert()
# To display the records...
import pickle
def display():
f=open("project.dat",'rb')
w=pickle.load(f)
print("The records are : ")
for i in w:
print(i)
f.close()
# To search and display for a particular record...
import pickle
def search():
f=open("project.dat",'rb')
v=pickle.load(f)
x=int(input("Enter the Roll no. to search : "))
c=0
for i in v:
if i[0]==x:
print("The record is : ",i)
c=1
if c==0:
print("Record not found")
f.close()
# To modify a record...
import pickle
def modify():
f=open("project.dat",'rb')
h=pickle.load(f)
x=int(input("Enter the Roll no. of record to modify : "))
a=0
for i in h:
if i[0]==x:
c=input("Press 'n' to modify name and 'm' to modify marks : ")
if c=='n' or c=='N':
n=input("Enter new name : ")
i[1]=n
print("Record updated")
elif c=='m' or c=='M':
m=float(input("Enter new marks : "))
i[2]=m
print("Record updated")
a=1
if a==0:
print("Record not found")
print("The new record is : ",h)
f.close()
# To delete a record...
import pickle
def delete():
f=open("project.dat",'rb+')
h=pickle.load(f)
x=int(input("Enter roll no. of record to delete : "))
l=[]
a=0
for i in h:
if i[0]!=x:
l.append(x)
a=1
print("Record deleted")
if a==0:
print("Record not found")
pickle.dump(l,f)
print("The new record is : ",l)
f.close()
# To copy all data into another file...
import pickle
def copy():
f=open("project.dat",'rb')
f1=open("new.dat",'wb')
h=pickle.load(f)
l=[]
for i in h:
l.append(i)
pickle.dump(l,f1)
print("New file created and record transferred ")
f.close()
f1.close()
# To copy data of students having marks>90...
import pickle
def data():
f=open("project.dat",'rb')
f1=open("new.dat",'wb')
h=pickle.load(f)
l=[]
for i in h:
if i[2]>90:
l.append(i)
pickle.dump(l,f1)
print("New file created and record of students having marks>90 transferred")
f.close()
f1.close()
# To copy data of students whose name starts with 'A'...
import pickle
def name():
f=open("project.dat",'rb')
f1=open("new.dat",'wb')
h=pickle.load(f)
l=[]
for i in h:
if i[1][0] in ('aA'):
l.append(i)
pickle.dump(l,f1)
print("New file created and record of students having name starting with 'A'
transferred")
f.close()
f1.close()
# To display average marks of all the students in the record...
import pickle
def average():
f=open("project.dat",'rb')
h=pickle.load(f)
a=0
c=0
for i in h:
a+=i[2]
c+=1
av=a/c
print("Average marks of all the students : ",av)
f.close()
# To display the total no. of records...
import pickle
def total():
f=open("project.dat",'rb')
h=pickle.load(f)
c=0
for i in h:
c+=1
print("Total no. of records are : ",c)
f.close()
# MENU...
while True:
print("""what do you want to perform?
1.Display all the records.
2.Search and display for a particular record.
3.Modify a record.
4.Delete a record.
5.Copy all the data into another file.
6.Copy data of students having marks > 90.
7.Copy data of students having name starting with 'A'.
8.Display Average marks of all the students.
9.Display total no. of records.""")
a=int(input("Enter here"))
if a==1:
display()
elif a==2:
search()
elif a==3:
modify()
elif a==4:
delete()
elif a==5:
copy()
elif a==6:
data()
elif a==7:
name()
elif a==8:
average()
elif a==9:
total()
else:
print("invalid value given")
break
restart=input("Do you want to perform again? (y/n) : ")
if restart=='y':
pass
elif restart=='n':
break
else:
print("value unacceptable")
print("Exiting...")
OUTPUTS...
Enter roll no. : 1
Enter name : Aarav Singh
Enter marks : 88
Do you want to enter more records? (y/n) : y
Enter roll no. : 2
Enter name : Meera Sharma
Enter marks : 92
Do you want to enter more records? (y/n) : y
Enter roll no. : 3
Enter name : Raj Patel
Enter marks : 75
Do you want to enter more records? (y/n) : y
Enter roll no. : 4
Enter name : Ananya Rao
Enter marks : 84
Do you want to enter more records? (y/n) : y
Enter roll no. : 5
Enter name : Ishan Gupta
Enter marks : 95
Do you want to enter more records? (y/n) : y
Enter roll no. : 6
Enter name : Neha Verma
Enter marks : 78
Do you want to enter more records? (y/n) : y
Enter roll no. : 7
Enter name : Arjun Malhotra
Enter marks : 84
Do you want to enter more records? (y/n) : y
Enter roll no. : 8
Enter name : Simran Kaur
Enter marks : 89
Do you want to enter more records? (y/n) : y
Enter roll no. : 9
Enter name : Rohan Iyer
Enter marks : 73
Do you want to enter more records? (y/n) : y
Enter roll no. : 10
Enter name : Priya Nair
Enter marks : 90
Do you want to enter more records? (y/n) : n
Data inserted in form of Records
what do you want to perform?
1.Display all the records.
2.Search and display for a particular record.
3.Modify a record.
4.Delete a record.
5.Copy all the data into another file.
6.Copy data of students having marks > 90.
7.Copy data of students having name starting with 'A'.
8.Display Average marks of all the students.
9.Display total no. of records.
Enter here1
The records are :
[1, 'Aarav Singh', 88.0]
[2, 'Meera Sharma', 92.0]
[3, 'Raj Patel', 75.0]
[4, 'Ananya Rao', 84.0]
[5, 'Ishan Gupta', 95.0]
[6, 'Neha Verma ', 78.0]
[7, 'Arjun Malhotra', 84.0]
[8, 'Simran Kaur', 89.0]
[9, 'Rohan Iyer', 73.0]
[10, 'Priya Nair', 90.0]
Do you want to perform again? (y/n) : y
what do you want to perform?
1.Display all the records.
2.Search and display for a particular record.
3.Modify a record.
4.Delete a record.
5.Copy all the data into another file.
6.Copy data of students having marks > 90.
7.Copy data of students having name starting with 'A'.
8.Display Average marks of all the students.
9.Display total no. of records.
Enter here2
Enter the Roll no. to search : 5
The record is : [5, 'Ishan Gupta', 95.0]
Do you want to perform again? (y/n) : y
what do you want to perform?
1.Display all the records.
2.Search and display for a particular record.
3.Modify a record.
4.Delete a record.
5.Copy all the data into another file.
6.Copy data of students having marks > 90.
7.Copy data of students having name starting with 'A'.
8.Display Average marks of all the students.
9.Display total no. of records.
Enter here3
Enter the Roll no. of record to modify : 2
Press 'n' to modify name and 'm' to modify marks : n
Enter new name : Sameer Verma
Record updated
Do you want to perform again? (y/n) : y
what do you want to perform?
1.Display all the records.
2.Search and display for a particular record.
3.Modify a record.
4.Delete a record.
5.Copy all the data into another file.
6.Copy data of students having marks > 90.
7.Copy data of students having name starting with 'A'.
8.Display Average marks of all the students.
9.Display total no. of records.
Enter here4
Enter roll no. of record to delete : 1
Record deleted
Do you want to perform again? (y/n) : y
what do you want to perform?
1.Display all the records.
2.Search and display for a particular record.
3.Modify a record.
4.Delete a record.
5.Copy all the data into another file.
6.Copy data of students having marks > 90.
7.Copy data of students having name starting with 'A'.
8.Display Average marks of all the students.
9.Display total no. of records.
Enter here5
New file created and record transferred
Do you want to perform again? (y/n) : y
what do you want to perform?
1.Display all the records.
2.Search and display for a particular record.
3.Modify a record.
4.Delete a record.
5.Copy all the data into another file.
6.Copy data of students having marks > 90.
7.Copy data of students having name starting with 'A'.
8.Display Average marks of all the students.
9.Display total no. of records.
Enter here6
New file created and record of students having marks>90 transferred
Do you want to perform again? (y/n) : y
what do you want to perform?
1.Display all the records.
2.Search and display for a particular record.
3.Modify a record.
4.Delete a record.
5.Copy all the data into another file.
6.Copy data of students having marks > 90.
7.Copy data of students having name starting with 'A'.
8.Display Average marks of all the students.
9.Display total no. of records.
Enter here7
New file created and record of students having name starting with 'A' transferred
Do you want to perform again? (y/n) : y
what do you want to perform?
1.Display all the records.
2.Search and display for a particular record.
3.Modify a record.
4.Delete a record.
5.Copy all the data into another file.
6.Copy data of students having marks > 90.
7.Copy data of students having name starting with 'A'.
8.Display Average marks of all the students.
9.Display total no. of records.
Enter here8
Average marks of all the students : 84.8
Do you want to perform again? (y/n) : y
what do you want to perform?
1.Display all the records.
2.Search and display for a particular record.
3.Modify a record.
4.Delete a record.
5.Copy all the data into another file.
6.Copy data of students having marks > 90.
7.Copy data of students having name starting with 'A'.
8.Display Average marks of all the students.
9.Display total no. of records.
Enter here9
Total no. of records are : 10
Do you want to perform again? (y/n) : n
ANSHIKA DIXIT
XII-B
11