Back to: Python Programming
0
Python can write content to a file.
text = "This line will end up being in a file called test.txt"
with open('c:/data/test.txt','w') as file:
file.write(text)
after the path, the default value is:
‘r’ – for read, this can be left off because it is the default.
‘w’ – writes a new file and if the file already exists, will overwrite it
‘a’ – will append text to the existing file an not overwrite what is there.
\n – will start a new line.
text = "\n This line has been appended."
with open('c:/data/test.txt','a') as file:
file.write(text)