0% found this document useful (0 votes)
0 views3 pages

Python File Handling Answers Class12 (1)

The document contains Python file handling exercises for Class 12, including functions to count words, lines, and vowel occurrences in various text files. It also includes tasks for filtering lines, replacing words, and copying content between files. Each exercise is presented with a function definition and an example of its usage.
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)
0 views3 pages

Python File Handling Answers Class12 (1)

The document contains Python file handling exercises for Class 12, including functions to count words, lines, and vowel occurrences in various text files. It also includes tasks for filtering lines, replacing words, and copying content between files. Each exercise is presented with a function definition and an example of its usage.
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/ 3

Python File Handling Questions - Class 12

Q2. Count total number of words in "WisdomQuotes.Txt"


def count_words():
with open("WisdomQuotes.Txt", "r") as file:
data = file.read()
words = data.split()
print("Total number of words in file:", len(words))

count_words()

Q3. Count how many times "my" occurs in "DATA.TXT"


def count_my():
with open("DATA.TXT", "r") as file:
text = file.read().lower()
words = text.split()
count = words.count("my")
print(f'"my" occurs {count} times')

count_my()

Q4. Count total lines and lines ending with "y"


def count_lines():
with open("student.txt", "r") as file:
lines = file.readlines()
total = len(lines)
ends_with_y = 0
for line in lines:
if line.strip().endswith("y"):
ends_with_y += 1
not_y = total - ends_with_y
print("The number of lines in file are:", total)
print("The number of lines ending with alphabet 'y' are:", ends_with_y)
print("The number of lines not ending with alphabet 'y' are:", not_y)

count_lines()

Q5. Count vowel occurrences in "MY_TEXT_FILE.TXT"


def VowelCount():
with open("MY_TEXT_FILE.TXT", "r") as file:
text = file.read().lower()
vowels = {'a':0, 'e':0, 'i':0, 'o':0, 'u':0}
for ch in text:
if ch in vowels:
vowels[ch] += 1
for v in vowels:
print(f"{v.upper()} occurs {vowels[v]} times")
VowelCount()

Q6. Copy all lines except those starting with "@"


def filter(oldfile, newfile):
with open(oldfile, "r") as f1, open(newfile, "w") as f2:
for line in f1:
if not line.startswith("@"):
f2.write(line)

filter("source.txt", "target.txt")

Q7. Replace "The" with "This" in a file


def replace_words():
with open("MY_TEXT_FILE.TXT", "r") as file:
text = file.read()
text = text.replace("The", "This")
with open("MY_TEXT_FILE.TXT", "w") as file:
file.write(text)

replace_words()

Q8. Count number of lines in "abc.txt"


def count_lines():
with open("abc.txt", "r") as file:
lines = file.readlines()
print("Number of lines:", len(lines))

count_lines()

Q9. Reverse words starting with I and copy to new file


def rev_text():
with open("content.txt", "r") as f1, open("new.txt", "w") as f2:
for line in f1:
words = line.strip().split()
new_words = []
for word in words:
if word.lower().startswith('i'):
new_words.append(word[::-1])
else:
new_words.append(word)
f2.write(" ".join(new_words) + "\n")

rev_text()

Q15. Filter words less than 4 characters from NewsLetter.TXT


def FilterWords():
c = 0
file = open('NewsLetter.TXT', 'r') # Statement 1
line = file.read() # Statement 2
word = line.split() # Statement 3
for c in word:
if len(c) < 4: # Statement 4
print(c)
file.close() # Statement 5

FilterWords()

10.

def copy_file():
with open("source.txt", "r") as src, open("destination.txt", "w") as dest:
for line in src:
dest.write(line)

copy_file()

You might also like