File Handling For 12
File Handling For 12
File handling is an essential concept in Python that allows programs to read, write, and manipulate
files. Files are used for persistent storage of data.
Types of Files
1. Text Files
2. Binary Files
File Operations
Opening a File
File Modes
Mode Description
'b' Binary mode. Add 'b' with modes for binary files (e.g., 'rb', 'wb').
Closing a File
Always close the file to free up resources. Use the close() method.
file_object.close()
3. content = file.read()
4. print(content)
5. file.close()
7. line = file.readline()
9. lines = file.readlines()
Writing to a File
3. file.write("Hello, World!")
4. file.close()
7. file.writelines(lines)
File Pointer
• The file pointer indicates the current position in the file.
• Use tell() to get the current position of the file pointer. Example:
• position = file.tell()
• print(position)
With Statement
The with statement is used to automatically close a file after its usage.
Example:
content = file.read()
print(content)
Binary files handle data in binary format. Example (writing binary data):
file.write(b"Binary Data")
data = file.read()
print(data)
try:
content = file.read()
except FileNotFoundError:
finally:
file.close()
Method Description
This concise summary covers key concepts required for the CBSE Class 12 Computer Science board
exam. Let me know if you'd like to add practice questions or detailed examples!
Here are 20 programming questions based on File Handling for CBSE Class 12 (083 – Computer
Science):
Basic Questions
1. Write a Python program to create a text file named example.txt and write the following lines
into it:
3. This is line 2.
5. Write a Python program to read and display the content of a file data.txt line by line.
6. Write a Python program to count the number of lines, words, and characters in a text file
named sample.txt.
7. Write a Python program to append the following data to an existing file notes.txt:
9. Write a program to check if a file named myfile.txt exists. If it exists, display its content;
otherwise, create the file and write "File created successfully."
Intermediate Questions
6. Write a Python program to read a file marks.txt containing student names and marks, and
display only the names of students who scored above 90.
7. Write a Python program to create a text file numbers.txt, write integers from 1 to 20, and
then read and print only the even numbers.
8. Write a Python program to replace all occurrences of the word "old" with "new" in a file
named document.txt.
9. Write a Python program to calculate the frequency of each word in a text file words.txt.
10. Write a Python program to copy the content of source.txt to a new file named
destination.txt.
Advanced Questions
11. Write a Python program to read a text file story.txt and display all lines that start with the
letter "A".
12. Write a Python program to merge the contents of two files, file1.txt and file2.txt, into a new
file merged.txt.
13. Write a Python program to read a binary file image.dat and display its content in
hexadecimal format.
14. Write a Python program to write a list of numbers into a binary file data.dat, then read the
file and calculate the sum of the numbers.
15. Write a Python program to remove all blank lines from a file named notes.txt.
16. Write a Python program to handle FileNotFoundError while trying to read a non-existent file
nonexistent.txt.
17. Write a Python program to safely open a file using a try-except-finally block, ensuring the file
is always closed, even if an error occurs.
Applications
18. Write a Python program to read student data (name, roll number, and marks) from a text file
students.txt and display the details of the student with the highest marks.
19. Write a Python program to read a CSV file products.csv containing product names and prices,
and display the details of products costing more than ₹500.
20. Write a Python program to read a file grades.txt containing student names and grades and
create two separate files: pass.txt (for grades ≥ 40) and fail.txt (for grades < 40).
These questions cover a range of difficulties and concepts, including reading/writing files, binary
files, text manipulation, exception handling, and file operations. Let me know if you need solutions or
additional questions!