Python Question Paper
Subject: Python Programming
Topic: File Handling
Total Questions: 10
Instructions:
- Write Python programs to solve the following problems.
- Use appropriate file handling modes and exception handling where necessary.
Section A: Basic File Operations (Q1 - Q3)
Q1. Write a Python program to create a text file named `sample.txt`, write your name and a
message into it, and then close the file.
Q2. Write a program to read and display the contents of `sample.txt`.
Q3. Write a Python script to append a new line `"This is an appended line"` to `sample.txt`
and display the updated content.
Section B: File Processing and Analysis (Q4 - Q7)
Q4. Write a Python program to count the total number of lines in a given file `sample.txt`.
Q5. Write a Python program that reads a file and prints only those lines that contain the
word “Python” (case-sensitive).
Q6. Write a Python program to count the number of words and characters in the file
`sample.txt`.
Q7. Write a program to copy the contents of `sample.txt` to another file `copy_sample.txt`.
Section C: Advanced File Handling (Q8 - Q10)
Q8. Write a Python program to display the last 3 lines of a text file.
Q9. Write a Python program that reads numbers from a file `numbers.txt`, one per line, and
writes only the even numbers to a new file `even_numbers.txt`.
Q10. Create a program that accepts user input (name, age, city) and stores it in a CSV file
`users.csv`. Ensure that every new entry is stored on a new line.