text = '''Some random data
'''
# 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)
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
with open('readme.txt', 'w') as f:
f.write('readme')
Code language: JavaScript (javascript)
file = open("msg.txt", "w")
file.write("my first file
")
file.write("This file
")
file.write("contains three lines
")
file.close()