Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
The very basics of text files
3. Text file modes
with statement and exception handling
Summary

Instruction

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.

Exercise

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.