Text_File_Questions_Class12
Text_File_Questions_Class12
Q1. Write a Python program to create a text file and write some lines into it.
Q2. Write a program to read a text file line by line and display each line.
for line in f:
print(line, end="")
Q3. Read a text file and count the total number of characters, words, and lines.
lines = f.readlines()
num_lines = len(lines)
Q1. Count the number of times a specific word appears in a text file.
word_to_count = "test"
count = 0
for line in f:
count += line.lower().split().count(word_to_count.lower())
print("Count:", count)
for line in f:
if word.lower() in line.lower():
print(line, end="")
3. File Modification
f2.write(f1.read())
Q2. Read a text file and write its content in reverse order (line-wise).
lines = f.readlines()
f.write(line)