The very basics of text files
3. Text file modes
with statement and exception handling


Good! The open() function can actually take a second (optional) argument: the mode in which the file should be opened. When the second argument is not provided, the default value is chosen as 'r' (read only). The most basic modes are:

  • 'r'read: a file can only be read,
  • 'w'write: a file with the given name is created (or overwritten if it already exists) for writing only,
  • 'a'append: a file with the given name is opened for adding new information at the end.


Modify the code from the previous exercise. Add 'r' as the second argument and run the code.

As you can see, nothing changes – 'r' (read only) is the default mode for opening files in Python.