Class 12 Python Text File Handling PYQs
Class 12 Python Text File Handling PYQs
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 .