0% found this document useful (0 votes)
62 views8 pages

Text File Programs

The document contains a series of Python programming tasks related to file handling, including creating, reading, writing, and manipulating text files. Key tasks include counting characters, words, and lines, filtering lines based on content, and modifying text by removing punctuation or capitalizing sentences. Each task is presented with example code snippets demonstrating the required functionality.

Uploaded by

Smritituku Datta
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)
62 views8 pages

Text File Programs

The document contains a series of Python programming tasks related to file handling, including creating, reading, writing, and manipulating text files. Key tasks include counting characters, words, and lines, filtering lines based on content, and modifying text by removing punctuation or capitalizing sentences. Each task is presented with example code snippets demonstrating the required functionality.

Uploaded by

Smritituku Datta
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/ 8

1. Write a program to create a text file named data.txt and write five lines of text into it.

In [1]: file = open('data.txt', 'w')


for i in range(5):
file.write("This is line " + str(i+1) + "\n")
file.close()

2. Write a Python program to open a file sample.txt and display its contents on the
screen.
In [ ]: file = open('sample.txt', 'r')
content = file.read()
print(content)
file.close()

3. Write a program to count the total number of characters, words, and lines in a text
file.
In [2]: file = open('data.txt', 'r')

line_count, word_count, char_count = 0,0,0

for line in file:


line_count += 1
words = line.split()
word_count += len(words)
char_count += len(line)

file.close()

print("Lines : ", line_count)


print("Words : ", word_count)
print("Characters: ", char_count)
Lines : 5
Words : 20
Characters: 75

4. Write a program to read a file and display only those lines which contain the word
“Python”.
In [ ]: file = open('sample.txt', 'r')

for line in file:


if 'Python' in line:
print(line.strip())

file.close()

5. Write a program to write a list of names into a file, each name on a new line.
In [6]: names = ['Subham', 'Dutta', 'Navya', 'Akshay', 'Srijeet', 'Soumybrata', 'Ayush', 'Adhrit']
file = open('names.txt', 'w')

for name in names:


file.write(name + "\n")

file.close()

6. Write a program that reads an existing file and copies all even-numbered lines into a
new file called even_lines.txt.
In [7]: file = open('data.txt', 'r')
lines = file.readlines()
file.close()

new_file = open('even_lines.txt','w')

for i in range(len(lines)):
if (i+1)%2 == 0:
new_file.write(lines[i])
new_file.close()

7. Write a Python program to find the longest word in a file.


In [9]: file = open('data.txt', 'r')

longest = ''
for line in file:
words = line.split()
for word in words:
if len(word) > len(longest):
longest = word
file.close()
print('Longest word: ', longest)

Longest word: Thisee

8. Write a program that reads a file and displays the lines in reverse order (last line first,
first line last).
In [10]: file = open('data.txt', 'r')
lines = file.readlines()
file.close()

for i in range(len(lines)-1, -1, -1):


print(lines[i].strip())

This is line 5
This is line 4
This is line 3
This is line 2
Thisee is line 1

9. Write a program that appends user input to an existing file until the user types STOP.
In [11]: file = open('data.txt', 'a')

while True:
text = input("Enter text (type STOP to end): ")
if text == 'STOP':
break
file.write(text + '\n')

file.close()

10. Write a program to remove all punctuation marks from a text file and write the clean
text into a new file.
In [ ]: file = open('data.txt', 'r')
text = file.read()
file.close()

clean_text = ''
for char in text:
if char not in '!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~':
clean_text = clean_text + char

file = open('clean_data.txt', 'w')


file.write(clean_text)
file.close()

In [13]: import string

file = open('data.txt', 'r')


text = file.read()
file.close()

clean_text = ''
for char in text:
if char not in string.punctuation:
clean_text = clean_text + char

file = open('clean_data.txt', 'w')


file.write(clean_text)
file.close()
11. Write a Python script that reads a text file and prints the frequency of each character
(excluding spaces and newlines).
In [14]: file = open('data.txt', 'r')
text = file.read()
file.close()

feq = {}

for char in text:


if char != ' ' and char != '\n':
if char in feq:
feq[char] += 1
else:
feq[char] = 1

for char in feq:


print(char, ' : ', feq[char])
T : 5
h : 6
i : 16
s : 11
e : 10
l : 5
n : 6
1 : 1
# : 5
$ : 1
2 : 1
3 : 1
4 : 1
5 : 1
P : 2
y : 1
t : 1
o : 5
! : 2
r : 2
g : 1
a : 1
m : 2
@ : 3
G : 1
d : 1
N : 1
c : 1
% : 3
A : 1
w : 1
~ : 4

12. Write a Python program that reads from input.txt and writes all lines containing the
word “error” into errors.txt.
In [ ]: infile = open('input.txt', 'r')
outfile = open('errors.txt', 'w')

for line in infile:


if 'error' in line.lower():
outfile.write(line)

infline.close()
outfile.close()

13. Write a program that merges the content of two text files into a third file.
In [ ]: f1 = open('file1.txt', 'r')
f2 = open('file2.txt', 'r')
f3 = open('file3.txt', 'w')

data1 = f1.read()
data2 = f2.read()

f3.write(data1 + '\n' + data2)

f1.close()
f2.close()
f3.close()

14. Write a program that checks whether a given word exists in a file or not
In [17]: word = input("Enter the word to search: ")
found = False

file = open('data.txt', 'r')

for line in file:


if word in line:
found = True
break

file.close()

if found:
print('Word found')
else:
print('Word not found')
Word not found

15. Create a program that reads an entire file and capitalizes the first letter of every
sentence. Save the output to a new file.
In [22]: file = open('data.txt', 'r')
text = file.read()
file.close()

new_text = ''
capitalize = True

for char in text:


if capitalize and char.isalpha():
new_text += char.upper()
capitalize = False
else:
new_text += char
if char == '.' or char == '!' or char == '?':
capitalize = True

file = open('capitalized.txt', 'w')


file.write(new_text)
file.close()

THE END

You might also like