Skip to content

Latest commit

 

History

History
42 lines (26 loc) · 1.12 KB

writing_files.rst

File metadata and controls

42 lines (26 loc) · 1.12 KB

Writing to a File

It should be noted that there are two methods for saving data to a file, and those are writing and appending. Writing to a file will write that bit of data, whatever it is, solely, to the file. This means if there was anything there before, it will be gone if you use write.

If you use append, then you will basically add to whatever is previously there.

Write

Here we have some simple text, but we also threw in a \n to denote a new line. This will start a newline in the file that we write to:

text = 'Sample Text to Save\nNew line!'

The next example notifies Python that you are opening the file exampleFile.txt, with the intention to write something to the file using 'w'.

saveFile = open('exampleFile.txt','w')

Now we need to write the text to the file:

saveFile.write(text)

It is important to remember to actually close the file, otherwise it will hang for a while and could cause problems in your script:

saveFile.close()