Opening and closing a file for writing: file and close

Like with a book, you need to do the following steps to write in a file

  • open the file (when it exists) or create it (when it does not yet exist)

  • write to the file

  • close the file

E.g.:

aFile = file("file.txt", "w")  # open or create
                               # the file,
                               # "w" indicates·
                               # for writing

   # write here to the file aFile

aFile.close()              # close and save
                           # (i.e. overwrite)
                           # the file
                           # for reading

   # do something without the file or
   # do something else with the file
   # e.g. reading from the file