Python Text File (Solved) Questions – For beginners
Q1. Write a program in python to read entire content of text file (“data.txt”)
Ans.
f = open("data.txt", 'r')
d = f.read( )
print(d)
Q2. Write a program in python to read first 5 characters from the text file(“data.txt”)
Ans.
f = open("data.txt", 'r')
d = f.read(5)
print(d)
Q3. Write a program in python to read first line from the text file(“data.txt”)
Ans.
f = open("data.txt", 'r')
d = f.readline( )
print(d)
Q4. Write a program in python to display number of lines in a text file(“data.txt”).
Ans.
f = open("data.txt", 'r')
d = f.readlines( )
print(len(d))
Q5. Write a program in python to display first line from the text file(“data.txt”) using readlines().
Ans.
f = open("data.txt", 'r')
d = f.readlines( )
print(d[0])
Q6. Write a program in python to display first character of all the lines from the text file(“data.txt”).
Ans.
f = open("data.txt", 'r')
d = f.readlines( )
for i in d:
print((i[0]))
Q7. Write a program in python to display all the lines from the text file(“data.txt”) with first character in uppercase.
Q8. Write a program in python to find the number of characters in a text file (“data.txt”).
Ans.
f = open("data.txt", 'r')
d = f.read( )
print(len(d))
NOTE: The above program will also count EOF character so displayed length of file is different from actual length of file
Q9. Write a program in python to find the number of characters in first line of text file (“data.txt”) using readline()
Ans.
f = open("data.txt", 'r')
d = f.readline( )
print(len(d))
Q10. Write a program in python to find the number of characters in first line of text file (“data.txt”) using readlines().
Ans.
f = open("data.txt", 'r')
d = f.readlines( )
print(len(d[0]))
Q11. Write a program in python to display last two characters of all the lines from the file(“data.txt”).
Ans.
f = open("data.txt", 'r')
d = f.readlines( )
for i in d:
print((i[-2:]))
Q12. Write a program to read all the characters from the file(“data.txt”) and display in uppercase.
Ans.
f = open("data.txt", 'r')
d = f.read( )
print(d.upper( ))
Q13. Write a program to count all the upper case characters from the file (“data.txt”).
Q14. Write a program to count number of spaces from the file (“data.txt”).
Q15. Write a program to count number of vowels in a file (“data.txt”).
Ans.
f = open("data.txt" , 'r')
l = f.read( )
c = 0
vow = "aeiouAEIOU"
for i in l:
if i in vow:
c = c + 1
print("Total vowels are = " , c)
Q16. Write a program to write following data in a file “data.txt”
I am learning python
I am writing this blog.
Welcome to my blog
Ans.
f = open("data.txt" , 'w')
f.write("I am learning python \n")
f.write("I am writing this blog. \n")
f.write("Welcome to my blog . ")
f.close( )
NOTE: While writing, we need to add EOF(\n) after every line.
Q17. Write a program to read data from “data.txt” and write in another file “dest.txt”
Ans.
f = open("data.txt" , 'r')
f1 = open("dest.txt" , 'w')
cd = f.read( )
f1.write(cd)
f1.close( )
f.close( )
Q18. Write a program to read all data from “data.txt” and write entire data in another file “dest.txt” except the spaces.
Ans.
f = open("data.txt" , 'r')
f1 = open("dest.txt" , 'w')
cd = f.read( )
for i in cd:
if(not i.isspace( ) or i=='\n')
f1.write(i)
f1.close( )
Q19. Write a program to read all data from “data.txt” and write in another file “dest.txt” except the vowels.
Ans.
f = open("data.txt" , 'r')
f1 = open("dest.txt" , 'w')
cd = f.read( )
vow = "aeiouAEIOU"
for i in cd:
if (not i in vow) :
f1.write(i)
f1.close( )
f.close( )
Q20. Write a program to read all data from “data.txt” and write alternate line (write first line, skip second line and so on)to another file “dest.txt”.
Ans.
f = open("data.txt" , 'r')
f1 = open("dest.txt" , 'w')
li = f.readlines( )
for i in range(len(li) ):
if i % 2 == 0:
f1.write(li[i])
f1.close( )
f.close( )
Q21. Write a program to read data from file “data.txt” and count the frequency of word input from user.
Ans.
f = open("data.txt" , 'r')
d = f.read( )
word = input("Enter the word to search: ")
print(d.count(word))
Q22. Write a program to read entire data from the file “data.txt” and write only those lines to file “dest.txt” which starts from word “The”.
Ans.
f = open("data.txt" , 'r')
f1 = open("dest.txt" , 'w')
l = f.readlines( )
for i in range(len(l)):
if (l[i][0:3] == "The"):
f1.write(l[i])
f1.close( )
f.close( )
Q23. Write a program to read entire data from file “data.txt” using readline() method.
Ans.
f = open(data.txt" , 'r')
while True:
l = f.readline( )
if not l:
break
print(l)
NOTE: We can read entire data of a file using read(), readline() and readlines()
Q24. Write a program to read the content from file “data.txt” and write to file “dest.txt” after changing the case(convert lower case to upper case and vice-versa)
Ans.
f = open("data.txt" , 'r')
f1 = open("dest.txt" , 'w')
data = f.read( )
f1.write(data.swapcase( ) )
f1.close( )
Q25. Write a program to create a list of 5 numbers (input from user) and write that list in a file “data.txt”.
Ans.
f = open("data.txt" , 'w')
l = list( )
c=0
while True:
n1 = input("Enter number")
l.append(n1)
c = c + 1
if c == 5:
break
f.writelines( l )
f.close( )
Q26. Write a program to create a list of 5 numbers(input from user) and write list in file “data.txt”. Now read the numbers from data.txt and write only even numbers to another file “dest.txt”.
Ans.
f = open("data.txt" , 'w')
l = list( )
c=0
while True:
n1 = input("Enter number")
l.append(n1)
c = c + 1
if c == 5:
break
for i in l:
f.write(i)
f.write('\n')
f.close( )
f = open("data.txt" , 'r')
f1 = open("dest.txt" , 'w')
d = f.readlines( )
for i in d:
if (int(i)%2 == 0):
f1.write(i)
f1.close( )
f.close( )
Q27. Write a program to read a file “data.txt” and replace word “school” by “college” and write in “dest.txt”
Ans.
f = open("data.txt" , 'r')
f1 = open("dest.txt" , 'w')
d = f.readlines( )
for i in d:
f1.write(i.replace("school" , "college"))
f1.close( )
Q28. Write a program to replace a word “school” to “college” in the same file(“data.txt”).
Ans.
f = open("data.txt" , 'r')
d = f.read( )
nd = d.replace("school" , "college")
f.close( )
f = open("data.txt" , 'w')
f.write(nd)
f.close( )
Disclaimer : I tried to give you the correct code of all the python Text File Handling programs, but if you feel that there is/are mistakes in the programs given above, you can directly contact me at csiplearninghub@gmail.com. Also Share your valuable feedback about the above Programs or any other suggestion so that I can give better content to you.
Class 12 Computer Science Sample Paper 2020-2021.
Class 12 Computer Science Sample Paper Marking Scheme
Class 12 Computer Science Test Series
It is really good for practicing good questions …thanks
Thank you So much.