File Modes in Python


☞ The following modes can be used to work with files.

Text FileBinary FileCSV FileDescription
rrbr
  • It is used for reading only a file.
  • It is the default mode.
  • If file does not exist FileNotFoundError
  • File pointer is placed at the beginning of the file.
wwbw
  • It is used for writing only into a file.
  • If file does not exist, it creates a new file.
  • If file exists, then overwrites it.
aaba
  • It is used for appending or writing into an existing file at the end.
  • If file does not exist, it creates a new file.
  • File pointer is at the end of the file if the file exists.
r+rb+r+
  • It is used for reading or writing both.
  • File pointer will be at the beginning of the file.
w+wb+w+
  • It is used for reading or writing both.
  • If file does not exist, it creates a new file.
  • If file exists, then overwrites it.
  • File pointer will be at the beginning of the file.
a+ab+a+
  • It is used for reading or writing both.
  • File pointer will be at the end of the file.
  • If file does not exist, it creates a new file.