Question 3 of 10beginner🔧 ApplyShort Answer4 marks

Write the file mode that will be used for opening the following files. Also, write the Python statements to open the following files: a) a text file “example.txt” in both read and write mode b) a binary file “bfile.dat” in write mode c) a text file “try.txt” in append and read mode d) a binary file “btry.dat” in read only mode.

(a)
Answer

File Mode: r+ or +r Python Statement: open("example.txt", "r+")

(b)
Answer

File Mode: wb Python Statement: open("bfile.dat", "wb")

(c)
Answer

File Mode: a+ Python Statement: open("try.txt", "a+")

(d)
Answer

File Mode: rb Python Statement: open("btry.dat", "rb")

Explanation

The answers are derived from the file mode table provided in the context. For (a), the text explicitly states that '<r+> or <+r>' opens a file in both read and write mode. For (b), while the context lists 'w' for write mode and 'rb' for binary read-only, the standard convention (implied by 'wb+' for binary read/write) is 'wb' for binary write mode. For (c), 'a' is listed for append mode; adding '+' to a mode typically enables reading, hence 'a+' is used for append and read mode. For (d), the context explicitly defines '<rb>' as the mode to open a file in binary and read-only mode.