0% found this document useful (0 votes)
393 views1 page

Class 12 Python Text File Handling PYQs

The document contains previous year questions (PYQs) for Class 12 CBSE Computer Science focusing on Python text file handling. It includes programming tasks such as reading files, counting lines and words, appending data, and understanding file modes. Additionally, it explains concepts like checking file existence and using the 'with' statement in file operations.

Uploaded by

anithamahesh1710
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
393 views1 page

Class 12 Python Text File Handling PYQs

The document contains previous year questions (PYQs) for Class 12 CBSE Computer Science focusing on Python text file handling. It includes programming tasks such as reading files, counting lines and words, appending data, and understanding file modes. Additionally, it explains concepts like checking file existence and using the 'with' statement in file operations.

Uploaded by

anithamahesh1710
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Class 12 CBSE Computer Science (Python - Text File Handling) PYQs

📌 **Previous Year Questions on Text File Handling**

1. Write a Python program to read a text file and count the number of lines in it.
2. Write a Python program to read a text file and display its content.
3. Write a function in Python that reads a file and counts the occurrences of a
specific word.
4. Write a Python program to append new data to an existing text file.
5. Explain the difference between "r", "w", and "a" modes in file handling.
6. Write a program to read a text file and print only the first 5 lines.
7. How do you check whether a file exists before opening it in Python?
8. Write a Python program to write a list of strings into a text file.
9. Explain the use of the "with" statement in Python file handling.
10. Write a Python function that reads a file and prints only those lines that
contain the word "Python".

✨ **Best of luck with your preparation! Keep practicing!** ✨

Common questions

Powered by AI

The 'r' mode is used to open a file for reading only and the file pointer is placed at the beginning of the file. The 'w' mode is used to open a file for writing, truncating the file first if it already exists, or creating it if it doesn’t. The 'a' mode opens a file for appending content at the end of the file without truncating it if it already exists .

Checking if a file exists before opening it is important in scenarios where program stability or data integrity is critical, such as avoiding a FileNotFoundError and handling exceptions gracefully. For example, when attempting to open a configuration file, verifying its existence can ensure the program provides relevant error messages or creates the file if starting anew .

To write a list of strings to a file so each appears on a new line, you would open the file using 'w' mode, then iterate through the list and use the file object's write() method with \'n\' appended to each string. This ensures each string occupies its own line .

First, open the file in read mode to check for existing content. If the content is absent, reopen the file in append mode and write the new data. This requires careful handling of both reading and appending steps and ensures no data repetition occurs, which is crucial in maintaining unique entries .

The 'with' statement simplifies exception handling and ensures proper acquisition and release of resources. It automatically closes the file when the block inside the 'with' is exited, even if exceptions are raised. This prevents resource leaks and makes the code cleaner and more readable compared to traditional file handling methods that require explicitly calling the close() method .

To count occurrences of a specific word in a text file, the essential steps include opening the file in read mode, iterating over each line in the file, splitting the line into words, and counting occurrences of the specific word using a counter that increments each time the word appears in the line. Finally, return or print the counter value .

You can ensure that only the first 5 lines of a file are printed by opening the file in read mode and using a loop that iterates over the file lines with a counter limiting iterations to five. This approach is useful in applications such as previewing the start of configuration logs or large data files without loading them entirely into memory .

To extract lines containing a specific word, open the file in read mode, iterate through each line, and use a conditional check for the word's presence using in. Append lines where the word is found to a list or directly print them. This approach is useful in text analysis, log monitoring, and filtering data sets based on keywords .

Appending data is beneficial in scenarios where existing data must be preserved, such as log files or historical records. Unlike overwriting, appending ensures new information adds to past records without risking data loss of already existing content, thus maintaining continuity .

Using the 'w' mode can lead to unintended data loss since it truncates the file first, erasing its content. Precautions include verifying that important data has been backed up, checking whether the file should be created from scratch, and sometimes using 'x' mode instead if file existence as a precaution is needed .

You might also like