188k views
0 votes
Identify the correct syntax used to write to a text file. a. file = open('My_data.txt', 'w') file.write('This file contains data') b. file = open('My_data.txt', 'w') file.write(100) c. file = open('My_data.txt', 'w') file.write([100, 200, 300]) d. file = open('My_data.txt', 'a') file.write(['hello', 'hi', 'hey'])

User MohanRaj S
by
3.9k points

1 Answer

4 votes

Answer:

file = open('My_data.txt', 'w')

file.write('This file contains data')

Step-by-step explanation:

Required

The correct syntax to write to file

From the question, we understand that we are to write to file and not append to the file.

This implies that the access mode will be w which stands for write

The first line of (a), (b) and (c) is correct.

(d) is incorrect

Solving further,

Only values of type string (i.e. str) can be written to file

With this, we can conclude that:

(a) is correct because 'This file contains data' is of type string

(b) and (c) are incorrect because, in (b), the value is of type integer and in (c), the type is list

User John Simons
by
3.9k points