Skip to content

Commit 7b44f29

Browse files
Merge pull request #1393 from Biancaa-R/master
Python(basic,file handling)
2 parents 393fb92 + 5a8ce74 commit 7b44f29

19 files changed

+427
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pickle
2+
def Bdelete():
3+
F= open("studrec.dat","rb")
4+
stud = pickle.load(F)
5+
F.close()
6+
print(stud)
7+
rno= int(input("Enter the roll number to be deleted"))
8+
F= open("studrec.dat","wb")
9+
rec= []
10+
for i in stud:
11+
if i[0] == rno:
12+
continue
13+
rec.append(i)
14+
pickle.dump(rec,F)
15+
F.close()
16+
Bdelete()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pickle
2+
3+
def binaryread():
4+
B=open("studrec.dat","rb")
5+
stud=pickle.load(B)
6+
print(stud)
7+
8+
# prints the whole record in nested list format
9+
print("contents of binary file")
10+
11+
for ch in stud:
12+
13+
print(ch) #prints one of the chosen rec in list
14+
15+
Rno=ch[0]
16+
Rname=ch[1] #due to unpacking the val not printed in list format
17+
Rmark=ch[2]
18+
19+
print(Rno, Rname,Rmark, end="\t")
20+
21+
B.close
22+
23+
binaryread()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#Updating records in a binary file
2+
3+
import pickle
4+
5+
def update():
6+
F=open("class.dat",'rb+')
7+
S=pickle.load(F)
8+
found=0
9+
rno=int(input("enter the roll number you want to update"))
10+
for i in S:
11+
if rno==i[0]:
12+
print("the currrent name is",i[1])
13+
i[1]=input("enter the new name")
14+
found=1
15+
break
16+
17+
if found==0:
18+
print("Record not found")
19+
20+
else:
21+
F.seek(0)
22+
pickle.dump(S,F)
23+
24+
F.close()
25+
26+
update()
27+
28+
F=open("class.dat","rb")
29+
val=pickle.load(F)
30+
print(val)
31+
F.close()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#updating records in a bnary file
2+
3+
import pickle
4+
5+
def update():
6+
F=open("studrec.dat","rb+")
7+
value=pickle.load(F)
8+
found=0
9+
roll=int(input("Enter the roll number of the record"))
10+
for i in value:
11+
if roll==i[0]:
12+
print("current name", i[1])
13+
print("current marks", i[2])
14+
i[1]=input("Enter the new name")
15+
i[2]=int(input("Enter the new marks"))
16+
found=1
17+
18+
if found==0:
19+
print("Record not found")
20+
21+
else:
22+
pickle.dump(value,F)
23+
F.seek(0)
24+
newval=pickle.load(F)
25+
print(newval)
26+
27+
F.close()
28+
update()
29+
30+
471 Bytes
Binary file not shown.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'''Amit is a monitor of class XII-A and he stored the record of all
2+
the students of his class in a file named “class.dat”.
3+
Structure of record is [roll number, name, percentage]. His computer
4+
teacher has assigned the following duty to Amit
5+
6+
Write a function remcount( ) to count the number of students who need
7+
remedial class (student who scored less than 40 percent)
8+
9+
10+
'''
11+
#also find no. of children who got top marks
12+
13+
import pickle
14+
F=open("class.dat",'ab')
15+
list=[[1,"Ramya",30],[2,"vaishnavi",60],[3,"anuya",40],[4,"kamala",30],[5,"anuraag",10],[6,"Reshi",77],[7,"Biancaa.R",100],[8,"sandhya",65]]
16+
17+
18+
pickle.dump(list,F)
19+
F.close()
20+
21+
def remcount():
22+
F=open("class.dat","rb")
23+
val=pickle.load(F)
24+
count=0
25+
26+
for i in val:
27+
if i[2]<=40:
28+
print(i,"eligible for remedial")
29+
count+=1
30+
print("the total number of students are",count)
31+
F.close()
32+
33+
remcount()
34+
35+
def firstmark():
36+
F=open("class.dat",'rb')
37+
val=pickle.load(F)
38+
main=[]
39+
count=0
40+
41+
for i in val:
42+
data=i[2]
43+
main.append(data)
44+
45+
top=max(main)
46+
print(top,"is the first mark")
47+
48+
F.seek(0)
49+
for i in val:
50+
if top==i[2]:
51+
print(i)
52+
print("congrats")
53+
count+=1
54+
55+
56+
print("the total number of students who secured top marks are",count)
57+
F.close()
58+
firstmark()
59+
60+
F=open("class.dat","rb")
61+
val=pickle.load(F)
62+
print(val)
63+
F.close()
64+
65+
66+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#binary file to search a given record
2+
3+
import pickle
4+
5+
def binary_search():
6+
F=open("studrec.dat",'rb')
7+
# your file path will be different
8+
value=pickle.load(F)
9+
search=0
10+
rno=int(input("Enter the roll number of the student"))
11+
12+
for i in value:
13+
if i[0]== rno:
14+
print("Record found successfully")
15+
print(i)
16+
search=1
17+
18+
if search==0:
19+
print("Sorry! record not found")
20+
F.close()
21+
22+
binary_search()
257 Bytes
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def longlines():
2+
F=open("story.txt","r")
3+
line=F.readlines()
4+
5+
for i in line:
6+
if len(i)<50:
7+
print(i,end=" ")
8+
9+
F.close()
10+
longlines()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
hello how are you
2+
what is your name
3+
do not worry everything is alright
4+
everything will be alright
5+
please dont loose hope
6+
Wonders are in the way
7+
Take a walk in the park
8+
In the end of the day you are more important than anything else.
9+
Many moments of happiness are waiting
10+
You are amazing!
11+
Its true believe.

0 commit comments

Comments
 (0)