Python File Handling Solutions (Q1 - Q35)
import csv
import pickle
# Q1: [Link]
def display_life_expectancy():
with open("[Link]", "r") as f:
reader = [Link](f)
for rec in reader:
if float(rec[1]) > 75:
print(rec)
def count_records():
with open("[Link]", "r") as f:
print("Total Records:", sum(1 for _ in f))
# Q2: Extract words
def extract_vowel_words():
with open("[Link]") as f:
for w in [Link]().split():
if w[0].lower() in "aeiou":
print(w)
def extract_hyphen_words():
with open("[Link]") as f:
for w in [Link]().split():
if "-" in w:
p = [Link]("-")
if len(p)==2 and len(p[0])==3 and len(p[1])==4:
print(w)
# Q3: Employee Binary File
def add_employee():
rec = [int(input("ID: ")), input("Name: "), input("Pos: "), float(input("Salary: "))]
with open("[Link]","ab") as f: [Link](rec,f)
def update_position():
data=[]
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
if r[3]>50000: r[2]="Team Lead"
[Link](r)
except EOFError: pass
with open("[Link]","wb") as f:
for r in data: [Link](r,f)
def display_non_team_leads():
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
if r[2]!="Team Lead": print(r)
except EOFError: pass
# Q4: [Link]
def display_large_cities():
with open("[Link]") as f:
for r in [Link](f):
if int(r[2])>200000: print(r)
def count_cities():
with open("[Link]") as f:
print("No of cities:", sum(1 for _ in f))
# Q5: [Link]
def NewTest():
rec=[int(input("Id: ")), input("Sub: "), int(input("Max: ")), int(input("Score: "))]
with open("[Link]","ab") as f: [Link](rec,f)
def UpdateMM(Sub):
data=[]
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
if r[1].lower()==[Link](): r[2]+=10
[Link](r)
except EOFError: pass
with open("[Link]","wb") as f:
for r in data: [Link](r,f)
def DisplayAvgMarks(Sub):
total=count=0
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
if r[1].lower()==[Link]():
total+=r[3]; count+=1
except EOFError: pass
print("Average:", total/count if count else "No records")
# Q6: [Link]
def gmail_words():
with open("[Link]") as f:
for w in [Link]().split():
if "@gmail" in w: print(w)
def five_char_words():
with open("[Link]") as f:
for w in [Link]().split():
if len(w)==5: print(w)
# Q7: Candidate Binary File
def add_candidate():
rec=[int(input("ID: ")), input("Name: "), input("Desig: "), float(input("Exp: "))]
with open("[Link]","ab") as f: [Link](rec,f)
def update_candidate():
data=[]
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
if r[3]>10: r[2]="Senior Manager"
[Link](r)
except EOFError: pass
with open("[Link]","wb") as f:
for r in data: [Link](r,f)
def display_non_seniors():
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
if r[2]!="Senior Manager": print(r)
except EOFError: pass
# Q8: [Link]
def count_lines_start_it():
with open("[Link]") as f:
print("Lines starting with 'It':", sum(1 for l in f if [Link]().startswith("It")))
def Transfer():
with open("[Link]") as src, open("[Link]","w") as dst:
for l in src:
if not [Link]("#"): [Link](l)
# Q9: [Link]
def SEARCH():
with open("[Link]") as f:
for r in [Link](f):
if int(r[2])>25000: print(r)
def COUNTROW():
with open("[Link]") as f:
print("Rows:", sum(1 for _ in f))
# Q10: Students
def AddStudents():
rec=[int(input("Roll: ")), input("Name: "), int(input("Marks: "))]
with open("[Link]","ab") as f: [Link](rec,f)
def GetStudents():
found=False
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
pct=r[2]
if pct>75:
print(r[1], pct,"%")
found=True
except EOFError: pass
if not found: print("No student above 75%")
# Q11 same as Q6
# Q12: [Link]
def display_pop():
with open("[Link]") as f:
for r in [Link](f):
if int(r[1])>5000000: print(r)
def count_country():
with open("[Link]") as f:
print("Count:", sum(1 for _ in f))
# Q13 same as Q7
# Q14: [Link]
def count_capital_words():
with open("[Link]") as f:
count=sum(1 for w in [Link]().split() if w[0].isupper())
print("Words starting with capital:", count)
def line_word_count():
with open("[Link]") as f:
for i,l in enumerate(f,1):
print("Line",i,":",len([Link]()))
# Q15: [Link]
def hindi_movies():
with open("[Link]") as f:
for r in [Link](f):
if r[3].lower()=="hindi": print(r)
def count_movies():
with open("[Link]") as f:
print("Count:", sum(1 for _ in f))
# Q16: Projects binary
def Prj_input():
rec=[int(input("ID: ")), input("Name: "), int(input("Members: ")), int(input("Duration: "))]
with open("Projects","ab") as f: [Link](rec,f)
def Prj_update():
data=[]
with open("Projects","rb") as f:
try:
while True:
r=[Link](f)
if r[2]>3: r[3]=3
[Link](r)
except EOFError: pass
with open("Projects","wb") as f:
for r in data: [Link](r,f)
def Prj_solo():
with open("Projects","rb") as f:
try:
while True:
r=[Link](f)
if r[2]==1: print(r)
except EOFError: pass
# Q17: [Link]
def CNTWORDS():
with open("[Link]") as f:
count=sum(1 for w in [Link]().split() if len(w)>=4)
return count
def LINECOUNT():
with open("[Link]") as f:
for l in f:
if "#" in l: print([Link]())
# Q18: [Link]
def high_salary():
with open("[Link]") as f:
for r in [Link](f):
if int(r[2])>50000: print(r)
def count_emp():
with open("[Link]") as f:
print("Records:", sum(1 for _ in f))
# Q19: Employee Binary
def INPUT_DATA():
rec=[int(input("ID: ")), input("Name: "), input("Job: "), float(input("Salary: "))]
with open("[Link]","ab") as f: [Link](rec,f)
def UPDATE_SAL():
data=[]
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
if r[2].lower()=="programmer": r[3]+=10000
[Link](r)
except EOFError: pass
with open("[Link]","wb") as f:
for r in data: [Link](r,f)
def READ_DATA():
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
if r[2]!="Engg": print(r)
except EOFError: pass
# Q20: Dictionary BRIGHT
def push(BRIGHT, admno, marks):
BRIGHT[admno]=marks
def avg_marks(BRIGHT, admno):
return sum(BRIGHT[admno])/len(BRIGHT[admno]) if admno in BRIGHT else 0
# Q21: [Link]
def write_file():
with open("[Link]","w",newline="") as f:
w=[Link](f)
[Link](["EmpID","EmpName","Desig","Dept"])
for i in range(5):
[Link]([i+1,"Name"+str(i),"Desig","Dept"])
def append_record():
with open("[Link]","a",newline="") as f:
w=[Link](f)
[Link]([6,"New","Manager","HR"])
# Q22: [Link]
def AddTeam():
rec=[input("Sport:"),input("Team:"),int(input("Players:"))]
with open("[Link]","ab") as f: [Link](rec,f)
def copyData():
count=0
with open("[Link]","rb") as f, open("[Link]","ab") as g:
try:
while True:
r=[Link](f)
if r[0].lower()=="basket ball":
[Link](r,g); count+=1
except EOFError: pass
return count
# Q23: [Link]
def count_A_lines():
with open("[Link]") as f:
print("Lines starting A:", sum(1 for l in f if [Link]().startswith("A")))
def DISPLAYWORDS():
with open("[Link]") as f:
for w in [Link]().split():
if len(w)<4: print(w)
# Q24: [Link]
def Add_Device():
rec=[int(input("ID: ")),input("Name:"),int(input("Price:"))]
with open("[Link]","a",newline="") as f:
[Link](f).writerow(rec)
def Count_Device():
with open("[Link]") as f:
print("Devices <1000:", sum(1 for r in [Link](f) if int(r[2])<1000))
# Q25: [Link]
def findType(ttype):
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
if r[2]==ttype: print(r)
except EOFError: pass
# Q26: [Link]
def Display():
with open("[Link]") as f:
for l in f:
if l[0].lower() not in "aeiou":
print([Link]())
def Count():
uc=lc=0
with open("[Link]") as f:
for ch in [Link]():
if [Link](): uc+=1
elif [Link](): lc+=1
print("Upper:",uc,"Lower:",lc)
# Q27: Fill blanks -> import csv, [Link](fr), reader, len(a[0])>5
# Q28: [Link]
def Update():
data=[]
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
marks=r[2]
if marks>=90: r[3]='A'
elif marks>=80: r[3]='B'
else: r[3]='C'
[Link](r)
except EOFError: pass
with open("[Link]","wb") as f:
for r in data: [Link](r,f)
# Q30: [Link]
def linecount():
with open("[Link]") as f:
print("Lines with P:", sum(1 for l in f if [Link]("P")))
def count_words_n():
with open("[Link]") as f:
count=sum(1 for w in [Link]().split() if [Link]("n"))
print("Words ending n:",count)
# Q31: [Link]
def add():
rec=[input("ID:"), input("Name:"), int(input("Price:"))]
with open("[Link]","a",newline="") as f:
[Link](f).writerow(rec)
def search():
with open("[Link]") as f:
for r in [Link](f):
if int(r[2])>10000: print(r)
# Q32: [Link]
def createFile():
rec=[int(input("No:")),input("Name:"),input("Author:"),int(input("Price:"))]
with open("[Link]","ab") as f: [Link](rec,f)
def CountRec(author):
c=0
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
if r[2].lower()==[Link](): c+=1
except EOFError: pass
return c
def displayAbove():
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
if r[3]>1000: print(r)
except EOFError: pass
# Q33: [Link]
def big_population():
with open("[Link]") as f:
for r in [Link](f):
if int(r[1])>5000000: print(r)
def count_records_joyful():
with open("[Link]") as f:
print("Total:", sum(1 for _ in f))
# Q34: Candidate Ankit
def add_candidate_ankit():
rec=[int(input("ID:")),input("Name:"),input("Desig:"),float(input("Exp:"))]
with open("[Link]","ab") as f: [Link](rec,f)
def display_exp5():
with open("[Link]","rb") as f:
try:
while True:
r=[Link](f)
if r[3]>5: print(r)
except EOFError: pass
# Q35: [Link]
def uld_count():
uc=lc=d=0
with open("[Link]") as f:
for ch in [Link]():
if [Link](): uc+=1
elif [Link](): lc+=1
elif [Link](): d+=1
print("Upper:",uc,"Lower:",lc,"Digits:",d)
def countwords():
with open("[Link]") as f:
count=sum(1 for w in [Link]().split() if len(w)>=5)
print("Words >=5 chars:",count)