Skip to content

Commit 90a1c11

Browse files
Merge pull request #2950 from NitkarshChourasia/master
1 File Handle
2 parents cd0de7b + daf0368 commit 90a1c11

25 files changed

+628
-105
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
STUDENTS_RECORD_FILE= "student_records.pkl"

1 File handle/File handle binary/Deleting record in a binary file.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

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
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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()
Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,66 @@
11
"""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”.
2+
the students of his class in a file named “student_records.pkl”.
33
Structure of record is [roll number, name, percentage]. His computer
44
teacher has assigned the following duty to Amit
55
66
Write a function remcount( ) to count the number of students who need
77
remedial class (student who scored less than 40 percent)
8+
and find the top students of the class.
89
9-
10+
We have to find weak students and bright students.
1011
"""
11-
# also find no. of children who got top marks
12+
13+
## Find bright students and weak students
14+
15+
from dotenv import load_dotenv
16+
import os
17+
18+
base = os.path.dirname(__file__)
19+
load_dotenv(os.path.join(base, ".env"))
20+
student_record = os.getenv("STUDENTS_RECORD_FILE")
1221

1322
import pickle
23+
import logging
24+
25+
# Define logger with info
26+
# import polar
1427

15-
list = [
16-
[1, "Ramya", 30],
17-
[2, "vaishnavi", 60],
18-
[3, "anuya", 40],
19-
[4, "kamala", 30],
20-
[5, "anuraag", 10],
21-
[6, "Reshi", 77],
22-
[7, "Biancaa.R", 100],
23-
[8, "sandhya", 65],
24-
]
2528

26-
with open("class.dat", "ab") as F:
27-
pickle.dump(list, F)
28-
F.close()
29+
## ! Unoptimised rehne de abhi ke liye
2930

3031

3132
def remcount():
32-
with open("class.dat", "rb") as F:
33+
with open(student_record, "rb") as F:
3334
val = pickle.load(F)
3435
count = 0
36+
weak_students = []
3537

36-
for i in val:
37-
if i[2] <= 40:
38-
print(f"{i} eligible for remedial")
38+
for student in val:
39+
if student[2] <= 40:
40+
print(f"{student} eligible for remedial")
41+
weak_students.append(student)
3942
count += 1
40-
print(f"the total number of students are {count}")
43+
print(f"the total number of weak students are {count}")
44+
print(f"The weak students are {weak_students}")
4145

42-
43-
remcount()
46+
# ! highest marks is the key here first marks
4447

4548

4649
def firstmark():
47-
with open("class.dat", "rb") as F:
50+
with open(student_record, "rb") as F:
4851
val = pickle.load(F)
4952
count = 0
5053
main = [i[2] for i in val]
5154

5255
top = max(main)
5356
print(top, "is the first mark")
5457

55-
F.seek(0)
5658
for i in val:
5759
if top == i[2]:
5860
print(f"{i}\ncongrats")
5961
count += 1
60-
61-
print("the total number of students who secured top marks are", count)
62+
print("The total number of students who secured top marks are", count)
6263

6364

65+
remcount()
6466
firstmark()
65-
66-
with open("class.dat", "rb") as F:
67-
val = pickle.load(F)
68-
print(val)
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()

1 File handle/File handle binary/search record in binary file.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
# binary file to search a given record
22

33
import pickle
4+
from dotenv import load_dotenv
45

56

6-
def binary_search():
7-
with open("studrec.dat", "rb") as F:
7+
def search():
8+
with open("student_records.pkl", "rb") as F:
89
# your file path will be different
9-
search = 0
10+
search = True
1011
rno = int(input("Enter the roll number of the student"))
1112

1213
for i in pickle.load(F):
1314
if i[0] == rno:
1415
print(f"Record found successfully\n{i}")
15-
search = 1
16+
search = False
1617

17-
if search == 0:
18+
if search:
1819
print("Sorry! record not found")
1920

2021

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Updating records in a binary file
2+
# ! Have a .env file please
3+
import pickle
4+
import os
5+
from dotenv import load_dotenv
6+
7+
base = os.path.dirname(__file__)
8+
load_dotenv(os.path.join(base, ".env"))
9+
student_record = os.getenv("STUDENTS_RECORD_FILE")
10+
11+
12+
def update():
13+
with open(student_record, "rb") as F:
14+
S = pickle.load(F)
15+
found = False
16+
rno = int(input("enter the roll number you want to update"))
17+
18+
for i in S:
19+
if rno == i[0]:
20+
print(f"the currrent name is {i[1]}")
21+
i[1] = input("enter the new name")
22+
found = True
23+
break
24+
25+
if found:
26+
print("Record not found")
27+
28+
with open(student_record, "wb") as F:
29+
pickle.dump(S, F)
30+
31+
32+
update()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
STUDENTS_RECORD_FILE= "student_records.pkl"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# updating records in a binary file
2+
3+
import pickle
4+
import os
5+
6+
base = os.path.dirname(__file__)
7+
from dotenv import load_dotenv
8+
9+
load_dotenv(os.path.join(base, ".env"))
10+
student_record = os.getenv("STUDENTS_RECORD_FILE")
11+
12+
## ! Understand how pandas works internally
13+
14+
15+
def update():
16+
with open(student_record, "rb") as File:
17+
value = pickle.load(File)
18+
found = False
19+
roll = int(input("Enter the roll number of the record"))
20+
21+
for i in value:
22+
if roll == i[0]:
23+
print(f"current name {i[1]}")
24+
print(f"current marks {i[2]}")
25+
i[1] = input("Enter the new name")
26+
i[2] = int(input("Enter the new marks"))
27+
found = True
28+
29+
if not found:
30+
print("Record not found")
31+
32+
with open(student_record, "wb") as File:
33+
pickle.dump(value, File)
34+
35+
36+
update()
37+
38+
# ! Instead of AB use WB?
39+
# ! It may have memory limits while updating large files but it would be good
40+
# ! Few lakhs records would be fine and wouldn't create any much of a significant issues

0 commit comments

Comments
 (0)