File Access mode In Python

Access mode determines the mode in which the file has to be opened ie. read, write append etc. *given below

Modes Description
r

1. Opens a file for reading only.

2. The file pointer is placed at the beginning of the file.

3. This is the default mode.

rb

1. Opens a file for reading only in binary format.

2. The file pointer is placed at the beginning of the file.

3. This is the default mode.

r+

1. Opens a file for both reading and writing.

2. The file pointer will be at the beginning of the file.

rb+

1. Opens a file for both reading and writing in binary format.

2. The file pointer will be at the beginning of the file.

w

1. Opens a file for writing only.

2. Overwrites the file if the file exists.

3. If the file does not exist, creates a new file for writing.

wb

1. Opens a file for writing only in binary format.

2. Overwrites the file if the file exists.

3. If the file does not exist, creates a new file for writing.

w+

1. Opens a file for both writing and reading.

2. Overwrites the existing file if the file exists.

3. If the file does not exist, creates a new file for reading and writing.

wb+

1. Opens a file for both writing and reading in binary format.

2. Overwrites the existing file if the file exists.

3. If the file does not exist, creates a new file for reading and writing.

a

1. Opens a file for appending.

2. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode.

3.If the file does not exist, it creates a new file for writing.

ab

1. Opens a file for appending in binary format.

2. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode.

3. If the file does not exist, it creates a new file for writing.

a+

1. Opens a file for both appending and reading.

2. The file pointer is at the end of the file if the file exists. The file opens in the append mode.

3.If the file does not exist, it creates a new file for reading and writing.

ab+

1. Opens a file for both appending and reading in binary format.

2.The file pointer is at the end of the file if the file exists. The file opens in the append mode.

3. If the file does not exist, it creates a new file for reading and writing.

Python Example

  1. fo=open("aks.txt",'w')
  2. print (fo.name)