Writing to Files

To write data to a file use the .write( ) method

fileName.write(“Data to write to the file...”)

When opening a file for writing use either

  • “a” opens a file for appending, creates the file if it does not exist. Adds new data to end of file.
  • “w” opens a file for writing, creates the file if it does not exist. Overwrites any existing data in file.

Let’s take a look at how this works.

video

Read page 57 – 58 of the textbook “Python for Beginners”.