Skip to content

Commit 094541f

Browse files
rename
1 parent 3a2e8ce commit 094541f

File tree

5 files changed

+102
-90
lines changed

5 files changed

+102
-90
lines changed

1 File handle/File handle binary/Update a binary file2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ def update():
2727

2828

2929
update()
30+
31+
# ! Instead of AB use WB?
32+
# ! It may have memory limits while updating large files but it would be good
33+
# ! Few lakhs records would be fine and wouln't create any much of a significant issues
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1-
import pickle
2-
import os
3-
import logging
4-
5-
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
6-
7-
8-
def b_read():
9-
# Opening a file & loading it
10-
if not os.path.exists("student_records.pkl"):
11-
logging.warning("File not found")
12-
return
13-
14-
with open("student_records.pkl", "rb") as F:
15-
student = pickle.load(F)
16-
logging.info("File opened successfully")
17-
logging.info("Records in the file are:")
18-
for i in student:
19-
logging.info(i)
20-
21-
def b_modify():
22-
# Deleting the Roll no. entered by user
23-
if not os.path.exists("student_records.pkl"):
24-
logging.warning("File not found")
25-
return
26-
roll_no = int(input("Enter the Roll No. to be deleted: "))
27-
student = 0
28-
with open("student_records.pkl", "rb") as F:
29-
student = pickle.load(F)
30-
31-
with open("student_records.pkl", "wb") as F:
32-
rec = [i for i in student if i[0] != roll_no]
33-
pickle.dump(rec, F)
34-
35-
36-
37-
b_read()
38-
b_modify()
1+
import logging
2+
import os
3+
import pickle
4+
5+
from dotenv import load_dotenv
6+
7+
base = os.path.dirname(__file__)
8+
load_dotenv(os.path.join(base, ".env"))
9+
10+
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
11+
student_record = os.getenv("STUDENTS_RECORD_FILE")
12+
13+
14+
def b_read():
15+
# Opening a file & loading it
16+
if not os.path.exists(student_record):
17+
logging.warning("File not found")
18+
return
19+
20+
with open(student_record, "rb") as F:
21+
student = pickle.load(F)
22+
logging.info("File opened successfully")
23+
logging.info("Records in the file are:")
24+
for i in student:
25+
logging.info(i)
26+
27+
28+
def b_modify():
29+
# Deleting the Roll no. entered by user
30+
if not os.path.exists(student_record):
31+
logging.warning("File not found")
32+
return
33+
roll_no = int(input("Enter the Roll No. to be deleted: "))
34+
student = 0
35+
with open(student_record, "rb") as F:
36+
student = pickle.load(F)
37+
38+
with open(student_record, "wb") as F:
39+
rec = [i for i in student if i[0] != roll_no]
40+
pickle.dump(rec, F)
41+
42+
43+
b_read()
44+
b_modify()

1 File handle/File handle binary/question 1 (elegible for remedial, top marks).py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
99
1010
"""
11+
1112
# also find no. of children who got top marks
1213

1314
import pickle
1415

16+
# ! Making a generator would be lovely a fake data generator for testing purposes
1517
list = [
1618
[1, "Ramya", 30],
1719
[2, "vaishnavi", 60],
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import pickle
2-
3-
4-
def binary_read():
5-
with open("studrec.dat", "rb") as b:
6-
stud = pickle.load(b)
7-
print(stud)
8-
9-
# prints the whole record in nested list format
10-
print("contents of binary file")
11-
12-
for ch in stud:
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-
22-
binary_read()
1+
import pickle
2+
3+
4+
def binary_read():
5+
with open("studrec.dat", "rb") as b:
6+
stud = pickle.load(b)
7+
print(stud)
8+
9+
# prints the whole record in nested list format
10+
print("contents of binary file")
11+
12+
for ch in stud:
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+
22+
binary_read()
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
# Updating records in a binary file
2-
3-
import pickle
4-
5-
6-
def update():
7-
with open("class.dat", "rb+") as F:
8-
S = pickle.load(F)
9-
found = False
10-
rno = int(input("enter the roll number you want to update"))
11-
12-
for i in S:
13-
if rno == i[0]:
14-
print(f"the currrent name is {i[1]}")
15-
i[1] = input("enter the new name")
16-
found = True
17-
break
18-
19-
if found:
20-
print("Record not found")
21-
22-
else:
23-
F.seek(0)
24-
pickle.dump(S, F)
25-
26-
27-
update()
28-
29-
with open("class.dat", "rb") as F:
30-
print(pickle.load(F))
1+
# Updating records in a binary file
2+
# ! Have a .env file please
3+
import pickle
4+
5+
6+
def update():
7+
with open("class.dat", "rb+") as F:
8+
S = pickle.load(F)
9+
found = False
10+
rno = int(input("enter the roll number you want to update"))
11+
12+
for i in S:
13+
if rno == i[0]:
14+
print(f"the currrent name is {i[1]}")
15+
i[1] = input("enter the new name")
16+
found = True
17+
break
18+
19+
if found:
20+
print("Record not found")
21+
22+
else:
23+
F.seek(0)
24+
pickle.dump(S, F)
25+
26+
27+
update()
28+
29+
with open("class.dat", "rb") as F:
30+
print(pickle.load(F))

0 commit comments

Comments
 (0)