0% found this document useful (0 votes)
5 views4 pages

Text File Handling Programs

The document provides 20 Python programs focused on text file handling, covering operations such as creating, reading, appending, and modifying text files. Each program demonstrates a specific functionality, such as counting lines, words, characters, and occurrences of specific words, as well as manipulating file content. The examples are straightforward and illustrate basic file operations using Python's built-in functions.

Uploaded by

shashikant1687
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Text File Handling Programs

The document provides 20 Python programs focused on text file handling, covering operations such as creating, reading, appending, and modifying text files. Each program demonstrates a specific functionality, such as counting lines, words, characters, and occurrences of specific words, as well as manipulating file content. The examples are straightforward and illustrate basic file operations using Python's built-in functions.

Uploaded by

shashikant1687
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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()

You might also like