xxxxxxxxxx
file = open("text.txt", "w")
file.write("Your text goes here")
file.close()
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
xxxxxxxxxx
text = '''Some random data\n'''
# write to data.txt
with open('data.txt', 'w') as f:
f.write(text)
# append to data.txt
with open('data.txt', 'a') as f:
f.write(text)
xxxxxxxxxx
with open('readme.txt', 'w') as f:
f.write('readme')
Code language: JavaScript (javascript)
xxxxxxxxxx
file = open("msg.txt", "w")
file.write("my first file\n")
file.write("This file\n")
file.write("contains three lines\n")
file.close()