20 Python Programs on Text File Handling
1. Create and write a text file
f = open("sample.txt", "w")
f.write("Hello, this is a text file.")
f.close()
2. Read a text file
f = open("sample.txt", "r")
print(f.read())
f.close()
3. Append text to a file
f = open("sample.txt", "a")
f.write("\nThis is an appended line.")
f.close()
4. Read file line by line
f = open("sample.txt", "r")
for line in f:
print(line.strip())
f.close()
5. Count the number of lines in a file
f = open("sample.txt", "r")
count = 0
for line in f:
count += 1
print("Total lines:", count)
f.close()
6. Count number of words in a file
f = open("sample.txt", "r")
words = f.read().split()
print("Total words:", len(words))
f.close()
7. Count number of characters in a file
f = open("sample.txt", "r")
data = f.read()
print("Total characters:", len(data))
f.close()
8. Count occurrence of a specific word
f = open("sample.txt", "r")
data = f.read()
word = "text"
count = data.count(word)
print(f"'{word}' occurs {count} times")
f.close()
9. Find the longest word in a file
f = open("sample.txt", "r")
words = f.read().split()
longest = max(words, key=len)
print("Longest word:", longest)
f.close()
10. Reverse the content of the file
f = open("sample.txt", "r")
data = f.read()
f.close()
f = open("sample.txt", "w")
f.write(data[::-1])
f.close()
11. Find and print lines starting with a specific word
f = open("sample.txt", "r")
for line in f:
if line.startswith("Hello"):
print(line.strip())
f.close()
12. Replace a word in the file
f = open("sample.txt", "r")
data = f.read().replace("text", "file")
f.close()
f = open("sample.txt", "w")
f.write(data)
f.close()
13. Display only the first 3 lines of a file
f = open("sample.txt", "r")
for i in range(3):
print(f.readline().strip())
f.close()
14. Display last 2 lines of a file
f = open("sample.txt", "r")
lines = f.readlines()
print(lines[-2:])
f.close()
15. Write even length words to another file
f = open("sample.txt", "r")
words = f.read().split()
f.close()
f2 = open("even_words.txt", "w")
for word in words:
if len(word) % 2 == 0:
f2.write(word + "\n")
f2.close()
16. Copy contents from one file to another
f1 = open("sample.txt", "r")
f2 = open("copy.txt", "w")
f2.write(f1.read())
f1.close()
f2.close()
17. Merge two files into a third
f1 = open("sample.txt", "r")
f2 = open("another.txt", "r")
f3 = open("merged.txt", "w")
f3.write(f1.read())
f3.write("\n")
f3.write(f2.read())
f1.close()
f2.close()
f3.close()
18. Remove all blank lines
f = open("sample.txt", "r")
lines = f.readlines()
f.close()
f = open("sample.txt", "w")
for line in lines:
if line.strip() != "":
f.write(line)
f.close()
19. Count uppercase and lowercase letters in a file
f = open("sample.txt", "r")
data = f.read()
upper = sum(1 for ch in data if ch.isupper())
lower = sum(1 for ch in data if ch.islower())
print("Uppercase:", upper)
print("Lowercase:", lower)
f.close()
20. Write every word in new line
f = open("sample.txt", "r")
words = f.read().split()
f.close()
f = open("sample.txt", "w")
for word in words:
f.write(word + "\n")
f.close()