PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Easy
Question 41
Q565 - I/O
The two basic, mutually exclusive, file open modes are named:
-
A
text and image
-
B
binary and text
-
C
binary and ternary
Reveal correct answer
Correct answer: B
Explanation
Topic: open() and its modes
Try it yourself:
- # First you have to run this to create the files:
- with open('alphabet.bin', 'wb') as f:
- f.write(bytes([65, 66, 67]))
- with open('alphabet.txt', 'wt') as f:
- f.write('ABC')
- with open('alphabet.bin', 'rb') as f:
- print(f.read()) # b'ABC'
- with open('alphabet.txt', 'rt') as f:
- print(f.read()) # ABC
Explanation:
You have to decide, whether you use binary or text mode:
https://www.w3schools.com/python/ref_func_open.asp
Q565 (Please refer to this number, if you want to write me about this question.)
A. Text and image are not the correct choices for the two basic, mutually exclusive file open modes in Python. While text is a valid file open mode for handling text data, image is not a standard file open mode in Python for handling image files. The correct choices are binary and text.
B. The correct choice is binary and text. These are the two basic, mutually exclusive file open modes in Python. The binary mode is used for reading and writing binary data, such as images or executable files, while the text mode is used for reading and writing text data, such as strings.
C. Binary and ternary are not the correct choices for the two basic, mutually exclusive file open modes in Python. Ternary is not a standard file open mode in Python. The correct choices are binary and text, where binary is used for binary data and text is used for text data.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
