Basic Python Read File | Write File programs
Data File Handling is an important topic for class 12 Computer Science Board Exams. Here you will find Solved Python read file and write file programs of Text File and Binary File
Basic Python Read File | Write File programs

Basic programs of Data File Handling (python read file and write to file) in python
Q1. Write a program to read entire content of file “story.txt”
Ans. f=open("story.txt" , "r") d = f.read( ) print(d)
Q2. Write a program to read first line of file “data.txt”.
Ans. f=open("data.txt" , "r") d = f.readline( ) print(d)
Q3. Write a program to count total number of lines in “sports.txt”.
Ans. f=open("sports.txt" , "r") d = f.readlines( ) print(len(d))
Q4. Write a program to display first alphabet of all the lines in “library.txt”.
Ans. f=open("library.txt" , "r") d = f.readlines( ) for i in d: print(i[0])
Q5. Write a program to read first 15 characters from the file “class.txt”.
Ans. f=open("class.txt" , "r") d = f.read(15) print(d)

Q6. Write a program to display only third line of file “test.txt”.
Ans. f=open("test.txt" , "r") d = f.readlines( ) print(d[2])
Q7. Write a program to display the length of file “len.txt”(Should not include EOL)
Ans. f=open("len.txt","r") d = f.read() L=0 for i in d: if i!="\n": L=L+1 print(L) OR f=open("len.txt","r") d = f.read() print(len(d)) #It will count the EOL also
Q8. Write python code to write the following lines in a file “story.txt”
Python read File and write File.
Weightage of python read file and write file is 10 marks.
Practice one program daily of python read file
Ans. f=open("story.txt",'w') f.write("Python read File and write File.\n") f.write("Weightage of python read file and write file is 10 marks.\n") f.write("Practice one program daily of python read file\n") f.close( )
Q9. Write a program to accept five numbers from the user and write in a file “number.txt” in separate lines.
Ans. f=open("number.txt",'w') for i in range(5): n=int(input("Enter number")) f.write(str(n)) #write( ) function takes string argument f.write("\n") f.close( )
Q10. Write a program to read content from a file “number.txt” and display the sum of numbers present in file.
for example : if the file contains the following data Total marks in exam is 25 My roll number is 15 My Address is 207(FF) Output : 22
Ans. f=open("number.txt" , "r") d = f.read() s=0 for i in d: if i.isdigit( )==True: s=s+int(i) print("Sum of numbers is :", s)

Q11. Write a program to create a list of 10 numbers and store it in a file “num.dat”
Ans. import pickle L = [ ] for i in range(10): num = int(input("Enter number")) L.append(num) f=open("num.dat" , 'wb') pickle.dump(L,f) f.close() #To verify your input, below is the code for reading file f=open("num.dat" , 'rb') d = pickle.load(f) print(d)
Q12. Write a program to write the following dictionary in a binary file named “detail.dat”.
D = {1 : ‘A’, 2 : ‘B’, 3 : ‘C’}
Ans. import pickle D = {1 : 'A', 2 : 'B', 3 : 'C'} f=open("detail.dat" , 'wb') pickle.dump(D,f) f.close() #To verify your input, below is the code for reading file f=open("detail.dat" , 'rb') d = pickle.load(f) print(d)
Q13. Write a program to store roll number and marks of five students in a dictionary and write the dictionary in a binary file “student.dat”
Ans. import pickle d={ } for i in range(5): rno = int(input("Enter Roll number")) marks = int(input("Enter marks")) d[rno] = marks f=open("student.dat" , 'wb') pickle.dump(d,f) f.close( ) #To verify your input, below is the code for reading file f=open("student.dat" , 'rb') d = pickle.load(f) print(d)
Q14. Write a program to add record of product (product number, product name) in a binary file “product.dat” (using list).
Ans. import pickle L =[ ] temp=[ ] pno = int(input("Enter product number")) pname = input("Enter product name") temp=[pno,pname] L.append(temp) #Use Loop to insert more records f=open("product.dat" , 'wb') pickle.dump(L,f) f.close( ) # Below is the code to verify the result f=open("product.dat" , 'rb') d = pickle.load(f) print(d)
Q15. Write a program to read all the records of product from “product.dat” (created above).
Ans. f=open("product.dat" , 'rb') d = pickle.load(f) print(d)

Q16. Write a program to search the record from the file “product.dat” created above according to the product number entered by the user.
Ans. f=open("product.dat" , 'rb') d = pickle.load(f) prno = int(input("Enter product number to search")) j = 0 k = 0 for i in d: if d[j][0]==prno: print(i) j = j + 1 k=1 break else: j = j+1 if k==0: print("Record not found")
Q17. Write a program to read the entire content from “record.csv”.
Ans. import csv f = open("record.csv" , "r") data = csv.reader(f) for row in data: print(row) f.close( )
Q18. Write a program to read the entire content from “data.csv” using “with open ( ) ” method.
Ans. import csv with open("data.csv" , "r") as f: data = csv.reader(f) for row in data: print(row) f.close( )
Q19. Write a program to count the number of records in “record.csv”.
Ans. import csv r = 0 with open("data.csv" , "r") as f: data = csv.reader(f) for row in data: r=r+1 print("Total records are :", r-1) #subtract 1 for excluding header f.close( )
Q20. Write a program to read the following data from “product.csv” and display the sum of all the price.
p_name , p_price Keyborad , 200 Mouse , 150 Harddisk , 2500 RAM , 2000
Ans. import csv r = 0 with open("data.csv" , "r") as f: next(f)# to skip header data = csv.reader(f) for i in data: r = int(i[1]) + r print("Sum of price is :", r) f.close( )
Important Links
100 Practice Questions on Python Fundamentals
120+ MySQL Practice Questions
90+ Practice Questions on List
50+ Output based Practice Questions
100 Practice Questions on String
70 Practice Questions on Loops
120 Practice Questions of Computer Network in Python
70 Practice Questions on if-else
40 Practice Questions on Data Structure
Class 12 Computer Science Sample Paper 2020-2021.
Class 12 Computer Science Sample Paper Marking Scheme
Class 12 Computer Science Test Series
Disclaimer : I tried to give the correct code of all the above questions of Data File Handling. If you feel or find some error in any code of the above questions of Data File Handling, please share the correction at csiplearninghub@gmail.com. Also share your feedback. Your feedback will motivate me and help me to give you better content