What are the different file processing modes supported by Python?

Python provides four modes to open files. The read-only (r), write-only (w), read-write (rw) and append mode (a). ‘r’ is used to open a file in read-only mode, ‘w’ is used to open a file in write-only mode, ‘rw’ is used to open in reading and write mode, ‘a’ is used to open a file in append mode. If the mode is not specified, by default file opens in read-only mode.

  • Read-only mode (r): Open a file for reading. It is the default mode.
  • Write-only mode (w): Open a file for writing. If the file contains data, data would be lost. Other a new file is created.
  • Read-Write mode (rw): Open a file for reading, write mode. It means updating mode.
  • Append mode (a): Open for writing, append to the end of the file, if the file exists.