i

Python Programming

Python File Opening Modes

Below is the list of the different modes of opening a file:

1    r-mode

For opening a file in read only. The file pointer is placed at beginning of the file. This is the default-mode.

2    rb-mode

For opening a file in read only in binary format. The file pointer is placed at beginning of the file. This is default mode.

3    r+-mode

For opening a file for both reading / writing. The file pointer placed at beginning of the file.

4    rb+-mode

For opening a file for both reading / writing in binary format. The file pointer placed at beginning of file.

5    w-mode

For opening a file for writing only. Overwrites the file if file exists. If the file does not exist, creates new file for writing purpose.

6    wb-mode

For opening a file for writing only in binary format. Generally oerwrites the file if file exists. If the file does not exist, creates the new file for writing.

7    w+-mode

For opening a file for both writing / reading. Overwrites the existing file if file exists. If file does not exist it creates new file for reading and writing.

8    wb+-mode

For opening a file for both writing and reading in binary format. Overwrites existing file if file exists. If the file don’t exist creates new file for reading and writing.

9    a-mode

For opening a file for appending. The file pointer is at end of file if file exists. That is if file is in append mode. If the file don’t exist creates a new file for writing.

10    ab-mode

For opening a file for appending in binary format. The file pointer is at end of file if file exists. That is, the file is in append-mode. If the file don’t exist creates a new file for writing.

11    a+-mode

For opening a file for appending in appending and reading. File pointer is at end of file if file exists and opens in the append mode. If file does not exist, it creates new file for reading and writing.

12    ab+-mode

For opening a file for both appending and reading in binary format. File pointer is at end of the file if file exists and file opens in the append mode. If file does not exist, it creates new file for reading and writing.