0% found this document useful (0 votes)
110 views

Python File Handling Programs - Text Files

Uploaded by

madhuvanthm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Python File Handling Programs - Text Files

Uploaded by

madhuvanthm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python File Handling Programs

Text File
Q1. Write a program in python to replace all word “the” by another word “them” in a file
“poem.txt”.
Ans.
f=open("poem.txt")
d=f.read()
d=d.replace("the","them")
f.close()
f=open("poem.txt","w")
f.write(d)
f.close()
Q2. Write a program in python to replace a character by another character in a file
“story.txt”. (Accept both the characters from the user)
Ans.
f = open("story.txt", "r")
d = f.read()
ch = input("Enter character to be replaced")
ch1 = input("Enter character that will replaced")
d = d.replace(ch, ch1)
f.close()
f = open("story.txt", "w")
f.write(d)
f.close()
Q3. Write a program in python to replace all the ‘a’ by ‘@’ in a file “data.txt”.
Ans.
f = open("data.txt", "r")
d = f.read()
d = d.replace('a', '@')
f.close()
f=open("data.txt", "w")
f.write(d)
f.close()
Q4. Write a program in python to read file “data.txt” and display only those lines whose
length is more than 40 characters.
Ans.
f=open("data.txt")
d=f.readlines()
for i in d:
if len(i)>40:
print(i)
Q5. Write a program in python to remove all duplicate lines from the file “story.txt”.
Ans.
f = open("story.txt", "r")
d = f.readlines()
m=[]
for i in d:
if i not in m:
m.append(i)
print(m)
f.close()
f = open("story.txt", "w")
for i in m:
f.write(i)
f.close()
Q6. Write a program in python to display only unique words from the file “story.txt”.
Ans.
f = open("story.txt", "r")
d = f.read()
d = d.split()
str = " "
m = []
for i in d:
if i not in str:
str=str+i
print(i, end=" ")
f.close()
Q7. Write a program in python to count the frequency of each vowels in a file “task.txt”.
Ans.
f = open("task.txt", "r")
d = f.read()
va=ve=vo=vu=vi=0
for i in d:
if i=='a' or i=='A':
va=va+1
if i=='e' or i=='E':
ve=ve+1
if i=='i' or i=='I':
vi=vi+1
if i=='o' or i=='O':
vo=vo+1
if i=='u' or i=='U':
vu=vu+1
print("Frequency of vowel \"a\" is", va)
print("Frequency of vowel \"e\" is", ve)
print("Frequency of vowel \"i\" is", vi)
print("Frequency of vowel \"o\" is", vo)
print("Frequency of vowel \"u\" is", vu)
Q8. Write a program in python to count those words whose length is more than 7
characters in a file “story.txt”.
Ans.
f=open("story.txt", "r")
d=f.read()
d=d.split()
c=0
for i in d:
if len(i)>7:
c=c+1
print("Total words are :", c)
Q9. Write a program in python to count those lines from the file “div.txt” which are
starting from ‘T’ or ‘M’.
Ans.
f=open("div.txt", "r")
d=f.readlines()
c=0
for i in d:
if i[0] == 'M' or i[0] == 'T':
c=c+1
print("Total lines are :", c)
Q10. Write a program in python to count those lines from the file “div.txt” which are not
starting from ‘M’.
Ans.
f=open("div.txt")
d=f.readlines()
c=0
for i in d:
if i[0] != 'M':
c=c+1
print("Total lines are :", c)
Q11. Write a program in python to display those words from a file “image.txt” which are
ending from alphabet ‘m’.
Ans.
f=open("image.txt")
d=f.read()
d=d.split()
c=0
for i in d:
if i[-1]=='m':
c=c+1
print("Total words are :", c)
Q12. Write a program in python to read all lines of file “data.txt” using readline() only.
Ans.
f=open("data.txt", "r")
str = " "
while str:
str = f.readline()
print(str, end = "")
f.close()
Q13. Write a program in Python to copy the entire content from file “data.txt” to
“story.txt”
Ans.
f = open("data.txt", "r")
f1 = open("story.txt", "w")
d = f.read()
f1.write(d)
f.close()
f1.close()
Q14. Write a program in Python to copy the alternate lines from file “data.txt” to
“story.txt”
Ans.
f = open("data.txt", "r")
f1 = open("story.txt", "w")
d = f.readlines()
for i in range(len(d)):
if i%2==0:
f1.write(d[i])
f.close()
f1.close()
Q15. Write a program in Python to read the entire content from file “data.txt” and copy
the contents to “story.txt” in upper case.
Ans.
f = open("data.txt", "r")
f1 = open("story.txt", "w")
d = f.readlines()
for i in d:
f1.write(i.upper())
f.close()
f1.close()

Q16. Write a program in Python to read the entire content from file “data.txt” and copy
only those words to “story.txt” which start from vowels.
Ans.
f = open("data.txt", "r")
f1 = open("story.txt", "w")
d = f.read()
d = d.lower()
word = d.split()
for i in word:
if i[0] in ['a', 'e', 'i', 'o', 'u']:
f1.write(i)
f.close()
f1.close()
Q17. Write a program in Python to read the entire content from file “data.txt” and copy
only those words in separate lines to “story.txt” which are starting from lower case
alphabets .
Ans.
f = open("data.txt", "r")
f1 = open("story.txt", "w")
d = f.read()
word = d.split()
for i in word:
if i[0].islower():
f1.write(i)
f1.write("\n")
f.close()
f1.close()
Q18. Write a program in Python to read file “data.txt” and copy only those lines to
“story.txt” which are starting from alphabets “A” or “T”.
Ans.
f = open("data.txt", "r")
f1 = open("story.txt", "w")
d = f.readlines()
for i in d:
if i[0] == "A" or i[0] == "T":
f1.write(i)
f.close()
f1.close()
Q19. Write a program in Python which display the longest word from file “star.txt”
Ans.
f = open("star.txt", "r")
d = f.read()
L = d.split()
longword = " "
for i in L:
if len(i) > len(longword):
longword = i
print("longest word is ,",longword)
f.close()
Q20. Write a program in Python which display the longest line from file “star.txt”
Ans.
f = open("star.txt", "r")
d = f.readlines()
longline = " "
for i in d:
if len(i) > len(longline):
longline = i
print("longest line is : ", longline)
f.close()

Q21. Write a program in Python to read the file “star.txt” and display the entire content
after removing leading and trailing spaces.
Ans.
f = open("star.txt", "r")
d = f.readlines()
for i in d:
print(i.strip())
f.close()
Q22. Write a program in python that read the content from file “sumit.txt” and display
all numbers.
Ans.
f = open("sumit.txt", "r")
d = f.read()
for i in d:
if i.isdigit():
print(i)
f.close()
Q23. Write a program in Python that display the second and second last line from the
file “life.txt”
Ans.
f = open("data.txt", "r")
d = f.readlines()
print("Second line is :",d[1])
print("Second last line is :",d[-2])
f.close()

You might also like