QUESTIONS
1. What is the difference between text files and binary files in Python?
2. Explain the purpose of the with statement in file handling.
3. What does the seek() and tell() method do?
4. Differentiate between read(), readline(), and readlines().
5. What are the different file access modes in Python? Give examples.
6. How does Python handle errors like "file not found"?
7. A file contains student records with names and marks. Write code to read and display
all records.
8. How would you append a new line to an existing text file without overwriting it?
9. Describe a way to count the number of lines in a text file using Python.
10. A text file contains words. Write a program to count the frequency of each word.
11. Explain how you would copy the contents of one file to another in Python.
12. Write a program to create a text file data.txt and write 5 lines to it.
13. Write a program that reads a file and prints only those lines which start with the
letter ‘A’.
14. Write a program to read a file and display the number of vowels in it.
15. Write a Python function to search for a specific word in a file and return the line
numbers where it appears.
16. Given a file marks.txt with student names and marks, write a program to
display names of students who scored more than 90.
17. Write a program to remove all punctuation from a given file and save the
cleaned text to a new file.
18. Write a program to merge the contents of two text files into a third file.
19. Write a program to reverse the content of each line in a file.
20. Given a large file, how would you read and process it line-by-line to avoid
memory issues?
Answers
1 Difference between text and binary files
→ Text files store data in readable text format. Binary files store data in binary (0s and 1s),
used for images, videos, etc.
2 Purpose of with statement
→ Ensures files are automatically closed after operations, even if errors occur.
3 Use of seek() and tell()
→ seek(offset) moves the file cursor to a position. tell() returns the current cursor
position.
4. Difference between read(), readline(), and readlines()
→ read() reads entire content, readline() reads one line, readlines() returns all lines as
a list.
5. File modes in Python
→ 'r': read, 'w': write, 'a': append, 'rb'/'wb': binary read/write.
6. Handling File Not Found error
→ Use try-except to catch FileNotFoundError.
7. with open('students.txt', 'r') as f:
for line in f:
print(line.strip())
8. ```python
with open('file.txt', 'a') as f:
f.write('\nNew line to append')
QUESTIONS
9. with open('file.txt', 'r') as f:
lines = f.readlines()
print('Number of lines:', len(lines))
10. ```python
from collections import Counter
with open('words.txt', 'r') as f:
words = f.read().split()
freq = Counter(words)
print(freq)
11. with open('source.txt', 'r') as src, open('dest.txt', 'w') as dest:
for line in src:
dest.write(line)
12. ```python
with open('data.txt', 'w') as f:
for i in range(5):
f.write(f'This is line {i+1}\n')
13. with open('file.txt', 'r') as f:
for line in f:
if line.startswith('A'):
print(line.strip())
14. ```python
vowels = 'aeiouAEIOU'
count = 0
with open('file.txt', 'r') as f:
for char in f.read():
if char in vowels:
count += 1
print('Vowels:', count)
15. def search_word(filename, word):
with open(filename, 'r') as f:
for i, line in enumerate(f, 1):
if word in line:
print(f'Line {i}: {line.strip()}')
16. ```python
with open('marks.txt', 'r') as f:
for line in f:
name, mark = line.split()
if int(mark) > 90:
print(name)
17. import string
with open('input.txt', 'r') as f:
text = f.read()
text = text.translate(str.maketrans('', '', string.punctuation))
with open('output.txt', 'w') as f:
f.write(text)
18. ```python
with open('file1.txt', 'r') as f1, open('file2.txt', 'r') as f2, open('merged.txt', 'w') as out:
out.write(f1.read())
QUESTIONS
out.write(f2.read())
19. with open('file.txt', 'r') as f, open('reversed.txt', 'w') as out:
for line in f:
out.write(line[::-1])
20. ```python
with open('largefile.txt', 'r') as f:
for line in f:
process(line) # Replace 'process' with your own function