Which of the following is not true for the Python command: File =…
2023
Which of the following is not true for the Python command: File = open('story.txt', 'a+')
- A.
More text can be written into 'story.txt
- B.
Content of the file 'story.txt' can be read.
- C.
Content of the file 'story.txt' can be modified.
- D.
Command generates an error, if the file 'story.txt. does not exist.
Attempted by 1827 students.
Show answer & explanation
Correct answer: D
Key idea: In Python, open('story.txt', 'a+') opens the file for both appending and reading. If the file does not exist, it is created. Writes are always appended to the end of the file.
You can write more text: 'a+' lets you append new data to the file. Any write goes to the file's end.
You can read the file: reading is allowed, but after opening the file the file pointer is at the end. Use file.seek(0) to read from the start.
Modifying existing content in place is not supported reliably: because append mode forces writes to the end, you cannot overwrite bytes already in the file. You can only add (append) new content.
If the file does not exist, it will be created. Therefore opening with 'a+' does not raise an error for a missing file.
Conclusion: The statement "Command generates an error, if the file 'story.txt' does not exist." is not true because 'a+' creates the file if it is missing. The other statements are correct when interpreted as: you can write more text (append), you can read (after seeking), and you can change the file by appending (but not overwrite existing content in place).