Opening and closing a file for reading: file and close

Like with a book, you need to do the following steps to read from a file

  • open the file

  • read from the file

  • close the file

E.g.:

f = file("file.txt", "r")  # open an existing file,
                           # "r" indicates opening
                           # for reading

   # read here from the file ....

f.close()                  # close the file
                           # for reading

   # do something without the file or
   # do something else with the file
   # e.g. writing to the file