Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 File handle/File handle binary/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
STUDENTS_RECORD_FILE= "student_records.pkl"

This file was deleted.

4 changes: 4 additions & 0 deletions 1 File handle/File handle binary/Update a binary file2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ def update():


update()

# ! Instead of AB use WB?
# ! It may have memory limits while updating large files but it would be good
# ! Few lakhs records would be fine and wouln't create any much of a significant issues
44 changes: 44 additions & 0 deletions 1 File handle/File handle binary/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import logging
import os
import pickle

from dotenv import load_dotenv

base = os.path.dirname(__file__)
load_dotenv(os.path.join(base, ".env"))

logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
student_record = os.getenv("STUDENTS_RECORD_FILE")


def b_read():
# Opening a file & loading it
if not os.path.exists(student_record):
logging.warning("File not found")
return

with open(student_record, "rb") as F:
student = pickle.load(F)
logging.info("File opened successfully")
logging.info("Records in the file are:")
for i in student:
logging.info(i)


def b_modify():
# Deleting the Roll no. entered by user
if not os.path.exists(student_record):
logging.warning("File not found")
return
roll_no = int(input("Enter the Roll No. to be deleted: "))
student = 0
with open(student_record, "rb") as F:
student = pickle.load(F)

with open(student_record, "wb") as F:
rec = [i for i in student if i[0] != roll_no]
pickle.dump(rec, F)


b_read()
b_modify()
Original file line number Diff line number Diff line change
@@ -1,68 +1,66 @@
"""Amit is a monitor of class XII-A and he stored the record of all
the students of his class in a file named “class.dat”.
the students of his class in a file named “student_records.pkl”.
Structure of record is [roll number, name, percentage]. His computer
teacher has assigned the following duty to Amit

Write a function remcount( ) to count the number of students who need
remedial class (student who scored less than 40 percent)
and find the top students of the class.


We have to find weak students and bright students.
"""
# also find no. of children who got top marks

## Find bright students and weak students

from dotenv import load_dotenv
import os

base = os.path.dirname(__file__)
load_dotenv(os.path.join(base, ".env"))
student_record = os.getenv("STUDENTS_RECORD_FILE")

import pickle
import logging

# Define logger with info
# import polar

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],
]

with open("class.dat", "ab") as F:
pickle.dump(list, F)
F.close()
## ! Unoptimised rehne de abhi ke liye


def remcount():
with open("class.dat", "rb") as F:
with open(student_record, "rb") as F:
val = pickle.load(F)
count = 0
weak_students = []

for i in val:
if i[2] <= 40:
print(f"{i} eligible for remedial")
for student in val:
if student[2] <= 40:
print(f"{student} eligible for remedial")
weak_students.append(student)
count += 1
print(f"the total number of students are {count}")
print(f"the total number of weak students are {count}")
print(f"The weak students are {weak_students}")


remcount()
# ! highest marks is the key here first marks


def firstmark():
with open("class.dat", "rb") as F:
with open(student_record, "rb") as F:
val = pickle.load(F)
count = 0
main = [i[2] for i in val]

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

F.seek(0)
for i in val:
if top == i[2]:
print(f"{i}\ncongrats")
count += 1

print("the total number of students who secured top marks are", count)
print("The total number of students who secured top marks are", count)


remcount()
firstmark()

with open("class.dat", "rb") as F:
val = pickle.load(F)
print(val)
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import pickle
def binary_read():
with open("studrec.dat", "rb") as b:
stud = pickle.load(b)
print(stud)
# prints the whole record in nested list format
print("contents of binary file")
for ch in stud:
print(ch) # prints one of the chosen rec in list
rno = ch[0]
rname = ch[1] # due to unpacking the val not printed in list format
rmark = ch[2]
print(rno, rname, rmark, end="\t")
binary_read()
import pickle


def binary_read():
with open("studrec.dat", "rb") as b:
stud = pickle.load(b)
print(stud)

# prints the whole record in nested list format
print("contents of binary file")

for ch in stud:
print(ch) # prints one of the chosen rec in list

rno = ch[0]
rname = ch[1] # due to unpacking the val not printed in list format
rmark = ch[2]

print(rno, rname, rmark, end="\t")


binary_read()
11 changes: 6 additions & 5 deletions 1 File handle/File handle binary/search record in binary file.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# binary file to search a given record

import pickle
from dotenv import load_dotenv


def binary_search():
with open("studrec.dat", "rb") as F:
def search():
with open("student_records.pkl", "rb") as F:
# your file path will be different
search = 0
search = True
rno = int(input("Enter the roll number of the student"))

for i in pickle.load(F):
if i[0] == rno:
print(f"Record found successfully\n{i}")
search = 1
search = False

if search == 0:
if search:
print("Sorry! record not found")


Expand Down
32 changes: 32 additions & 0 deletions 1 File handle/File handle binary/update2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Updating records in a binary file
# ! Have a .env file please
import pickle
import os
from dotenv import load_dotenv

base = os.path.dirname(__file__)
load_dotenv(os.path.join(base, ".env"))
student_record = os.getenv("STUDENTS_RECORD_FILE")


def update():
with open(student_record, "rb") as F:
S = pickle.load(F)
found = False
rno = int(input("enter the roll number you want to update"))

for i in S:
if rno == i[0]:
print(f"the currrent name is {i[1]}")
i[1] = input("enter the new name")
found = True
break

if found:
print("Record not found")

with open(student_record, "wb") as F:
pickle.dump(S, F)


update()
1 change: 1 addition & 0 deletions file_handle/File handle binary/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
STUDENTS_RECORD_FILE= "student_records.pkl"
40 changes: 40 additions & 0 deletions file_handle/File handle binary/Update a binary file2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# updating records in a binary file

import pickle
import os

base = os.path.dirname(__file__)
from dotenv import load_dotenv

load_dotenv(os.path.join(base, ".env"))
student_record = os.getenv("STUDENTS_RECORD_FILE")

## ! Understand how pandas works internally


def update():
with open(student_record, "rb") as File:
value = pickle.load(File)
found = False
roll = int(input("Enter the roll number of the record"))

for i in value:
if roll == i[0]:
print(f"current name {i[1]}")
print(f"current marks {i[2]}")
i[1] = input("Enter the new name")
i[2] = int(input("Enter the new marks"))
found = True

if not found:
print("Record not found")

with open(student_record, "wb") as File:
pickle.dump(value, File)


update()

# ! Instead of AB use WB?
# ! It may have memory limits while updating large files but it would be good
# ! Few lakhs records would be fine and wouldn't create any much of a significant issues
44 changes: 44 additions & 0 deletions file_handle/File handle binary/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import logging
import os
import pickle

from dotenv import load_dotenv

base = os.path.dirname(__file__)
load_dotenv(os.path.join(base, ".env"))

logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
student_record = os.getenv("STUDENTS_RECORD_FILE")


def b_read():
# Opening a file & loading it
if not os.path.exists(student_record):
logging.warning("File not found")
return

with open(student_record, "rb") as F:
student = pickle.load(F)
logging.info("File opened successfully")
logging.info("Records in the file are:")
for i in student:
logging.info(i)


def b_modify():
# Deleting the Roll no. entered by user
if not os.path.exists(student_record):
logging.warning("File not found")
return
roll_no = int(input("Enter the Roll No. to be deleted: "))
student = 0
with open(student_record, "rb") as F:
student = pickle.load(F)

with open(student_record, "wb") as F:
rec = [i for i in student if i[0] != roll_no]
pickle.dump(rec, F)


b_read()
b_modify()
Loading
Loading