Skip to content
Open
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
# updating records in a binary file
import pickle
def update():
with open("studrec.dat", "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")
else:
pickle.dump(value, File)
File.seek(0)
print(pickle.load(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 wouln't create any much of a significant issues
# updating records in a binary file

import pickle


def update():
with open("studrec.dat", "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")

else:
pickle.dump(value, File)
File.seek(0)
print(pickle.load(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 wouln't create any much of a significant issues
Original file line number Diff line number Diff line change
@@ -1,66 +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 “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.
"""
## 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
## ! Unoptimised rehne de abhi ke liye
def remcount():
with open(student_record, "rb") as F:
val = pickle.load(F)
count = 0
weak_students = []
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 weak students are {count}")
print(f"The weak students are {weak_students}")
# ! highest marks is the key here first marks
def firstmark():
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")
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)
remcount()
firstmark()
"""Amit is a monitor of class XII-A and he stored the record of all
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.
"""

## 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


## ! Unoptimised rehne de abhi ke liye


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

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 weak students are {count}")
print(f"The weak students are {weak_students}")

# ! highest marks is the key here first marks


def firstmark():
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")

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)


remcount()
firstmark()
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# binary file to search a given record
import pickle
from dotenv import load_dotenv
def search():
with open("student_records.pkl", "rb") as F:
# your file path will be different
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 = False
if search:
print("Sorry! record not found")
binary_search()
# binary file to search a given record

import pickle
from dotenv import load_dotenv


def search():
with open("student_records.pkl", "rb") as F:
# your file path will be different
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 = False

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


binary_search()
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import os
import time
file_name = input("Enter the file name to create:- ")
print(file_name)
def write_to_file(file_name):
if os.path.exists(file_name):
print(f"Error: {file_name} already exists.")
return
with open(file_name, "a") as F:
while True:
text = input("enter any text to add in the file:- ")
F.write(f"{text}\n")
choice = input("Do you want to enter more, y/n").lower()
if choice == "n":
break
def longlines():
with open(file_name, encoding="utf-8") as F:
lines = F.readlines()
lines_less_than_50 = list(filter(lambda line: len(line) < 50, lines))
if not lines_less_than_50:
print("There is no line which is less than 50")
else:
for i in lines_less_than_50:
print(i, end="\t")
if __name__ == "__main__":
write_to_file(file_name)
time.sleep(1)
longlines()
import os
import time

file_name = input("Enter the file name to create:- ")

print(file_name)


def write_to_file(file_name):
if os.path.exists(file_name):
print(f"Error: {file_name} already exists.")
return

with open(file_name, "a") as F:
while True:
text = input("enter any text to add in the file:- ")
F.write(f"{text}\n")
choice = input("Do you want to enter more, y/n").lower()
if choice == "n":
break


def longlines():
with open(file_name, encoding="utf-8") as F:
lines = F.readlines()
lines_less_than_50 = list(filter(lambda line: len(line) < 50, lines))

if not lines_less_than_50:
print("There is no line which is less than 50")
else:
for i in lines_less_than_50:
print(i, end="\t")


if __name__ == "__main__":
write_to_file(file_name)
time.sleep(1)
longlines()
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
hello how are you
what is your name
do not worry everything is alright
everything will be alright
please don't lose hope
Wonders are on the way
Take a walk in the park
At the end of the day you are more important than anything else.
Many moments of happiness are waiting
You are amazing!
hello how are you
what is your name
do not worry everything is alright
everything will be alright
please don't lose hope
Wonders are on the way
Take a walk in the park
At the end of the day you are more important than anything else.
Many moments of happiness are waiting
You are amazing!
If you truly believe.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# practicing with streams
import sys
sys.stdout.write("Enter the name of the file")
file = sys.stdin.readline()
with open(
file.strip(),
) as F:
while True:
ch = F.readlines()
for i in ch: # ch is the whole file,for i in ch gives lines, for j in i gives letters,for j in i.split gives words
print(i, end="")
else:
sys.stderr.write("End of file reached")
break
# practicing with streams
import sys

sys.stdout.write("Enter the name of the file")
file = sys.stdin.readline()

with open(
file.strip(),
) as F:
while True:
ch = F.readlines()
for i in ch: # ch is the whole file,for i in ch gives lines, for j in i gives letters,for j in i.split gives words
print(i, end="")
else:
sys.stderr.write("End of file reached")
break
Loading