Opening and Writing Text to a Text File |Section 5|Celestial Warrior

1 year ago
1

Please write some code thatcreates a text file and writesthe items oflist numbers = [1, 2, 3]to that text file.

The text file content should look like following:

1
2
3

Note that the write() method takes only string datatypes so you may need to convert integers to strings in order to be able to write them in a file.

numbers = [1, 2, 3]
file = open("numbers.txt", "w")
for i in numbers:
file.write(str(i) + "\n")
file.close()

Loading comments...