[Type B] File Handling – Chapter 5 Sumita Arora
Table of Contents
9. If you use the code of Q.8 with pl.txt (created in solved problem 16), what would be its output?
p1.txt from solved problem 16 contains:
Better than Heaven or ArcadiaI love thee, O my India!And thy loveI shall giveTo every brother nation that lives.
Answer: The output of the provided code using pl.txt
:
Output
1
Explanation:
The code reads the content of pl.txt
, counts the number of lines (nl
variable), and prints the count after reading all lines in the file. In the pl.txt
file, there is one line, so the output is 1
.
Note: If the file ‘pl.txt’ does not exist in the same directory as the Python script, the code will raise a FileNotFoundError
. You may want to add error handling code to handle such cases in a real-world scenario.
Good work posting these questions in detail!