File Handling XII
File Handling XII
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))
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")
Search operation
#Search and display marks between
50 and 70
#Display the name having grade A.