0% found this document useful (0 votes)
57 views45 pages

File Handling XII

Methods are presented for storing student data like roll number, name, and marks in a text file using functions like write, writelines. The document also shows how to append new records to a binary file using pickle dump and load functions. Examples of searching, updating, and reading data from binary files using
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views45 pages

File Handling XII

Methods are presented for storing student data like roll number, name, and marks in a text file using functions like write, writelines. The document also shows how to append new records to a binary file using pickle dump and load functions. Examples of searching, updating, and reading data from binary files using
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

FILE

HANDLING XII
Prepared By: Hardeep Dhillon
HOW TO OPEN A TEXT FILE
WRITE FUNCTION
with open("sum.txt","w") as f:
a=int(input("enter a: "))
b=int(input("enter b: "))
c=a+b
f.write("enter a: "+str(a))
f.write("\t""enter b: "+str(b))
f.write("\n""sum: "+str(a+b))

EXAMPLE OF ADD PROGRAM


Writelines () function
n=int(input("enter number of students: "))
with open("marks_det.txt","w+")as f:
for i in range (n):
roll_no=int(input("enter roll_no"))
name=input("enter name: ")
marks=float(input("enter marks: "))
rec=str(roll_no)+","+name+","+str(marks)+"\n"
print(f.write(rec))
f.seek(0)
data=f.read()
print(data)

WAP to hold student’s data in a file


like roll no, name, marks etc.
def read():
with open ("poem.txt") as f:
while True:
s=f.readline()
if s=="":
break
else:
print(s)
read()

Solution
Write a method in python that read
to lines from text file “poem.txt” and
displaying those line starting from
letter F.

Important
def read():
with open ("poem.txt") as f:
lines=f.readlines()
for i in lines:
if i[0]=="a":
print(i)
read()

Solution
1. Write a method in python that read to lines from text
file “poem.txt” and counting lines those are starting
from letter F.
2. Write a method in python that read to lines from text
file “poem.txt” and print lines which are starting from
letter F or D.
3. Write a method in python BIGLINES() that read to lines
from text file “poem.txt” and print lines which are
bigger than 50 characters

Function or Method
Here pickle is module and dump is the
function.
Pickle is module and load is function.
import pickle
f=open("binarywrite.dat",'wb')
list=['physics','maths','bio',4,'english']
pickle.dump(list,f)
f.close()
f=open("binarywrite.dat",'rb')
data=pickle.load(f)
print(data)
f.close()
print("file written successfully")

Example of use of pickle, dump, load


import pickle
#append is same as write use ab in place wb def read():
#it will append
def append(): f=open("bin.dat","rb")
f = open("bin.dat", "ab") while True:
record = []
while True:
try:
rno = int(input("rno: ")) s=pickle.load(f)
name = input("name: ")
marks = float(input("marks: "))
for i in s:
data = [rno, name, marks] print(i)
record.append(data)
except EOFError:
ch = input("enter choice y/n : ")
if ch == "n": break
break
f.close()
pickle.dump(record, f)
append() read()
def update():
f=open("bin.dat","rb+")
if found==0:
s=pickle.load(f)
found=0 print("record not found")
rno=int(input("enter roll to be update: ")) else:
for i in s: f.seek(0)
if i[0]==rno: pickle.dump(s,f)
print("name already present is: ",i[1]) f.close()
i[1]=input("enter new name: ")
found=1
break

Update operation on binary file


def search():
f=open("bin.dat","rb")
s=pickle.load(f)
found=0
rno=int(input("enter rno: "))
for i in s:
if i[0]==rno:
print(i)
found=1
if found==0:
print("record not found:")
search()

Search operation
#Search and display marks between
50 and 70
#Display the name having grade A.

You might also like