Consider the following Python command: File = open('Myfile', 'a') Which of the…
2023
Consider the following Python command: File = open('Myfile', 'a') Which of the following option cannot be true?
- A.
'Myfile' can be a text file
- B.
'Myfile' can be a csv file
- C.
'Myfile' can be a binary file
- D.
'Myfile' will be created if not exist
Attempted by 1754 students.
Show answer & explanation
Correct answer: C
Answer: The statement "'Myfile' can be a binary file" cannot be true when opened with mode 'a' alone.
Explanation:
Append mode and text vs binary: mode 'a' opens the file for appending in text mode by default. To open for binary append you must include 'b' in the mode (for example 'ab').
Why the binary statement is incorrect: without the 'b' flag, Python handles the file as text and expects str objects for writing. Binary files require bytes and the binary mode to avoid encoding/decoding issues.
Other statements are true: a CSV file is a text format so it can be appended with mode 'a'. A plain text file can be appended. Also, append mode ('a') creates the file if it does not exist.
Practical tip: when writing CSV with Python's csv module, use open(..., mode='a', newline='') to avoid extra blank lines on some platforms.