File Opening Modes in Python

File Opening Modes in Python

We have the syntax:

var1 = open(file_name [, access_mode] [, buffering])

file_name — the name of our file

access_mode — the mode of opening our file

+

It is the extended mode where we can read and write at the same time.

b

It is the binary mode where we can read and write block bytes, not text (text mode)

buffering — the place in a computer memory to temporary store date before moving it to other place (here: to the file). It’s especially important for big data. we can switch off buffering by setting it to 0. If the value is 1, line buffering will be performed during accessing our file. If the value > 1, the buffering will be with indicated buffer size. If the value < 0, the buffer size is the system default that is a default behavior.

We can do anything with this variable var1 using a dot operator:

var1.read()

var1.write(“string”)

x

Creates a file if the file doesn’t exist.

>>> t = open(“D:/derek.txt”, mode=’x’)
>>>

If the file exists, we get the error:

>>> t = open(“D:/derek.txt”, mode=’x’)
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
FileExistsError: [Errno 17] File exists: ‘D:/derek.txt’

r

Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.

>>> t = open(“D:/derek.txt”, mode=’r’)
>>> t.read()
‘We have a new ship\nMurek’
>>> t.write(“And a boat”)
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
io.UnsupportedOperation: not writable
>>>

rb

Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.

>>> t = open(“D:/derek.txt”, mode=’rb’)
>>> t.read()
b’We have a new ship\r\nMurek’
>>> t.write(“And a boat”)
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
io.UnsupportedOperation: write
>>>

We see a “b” before the text (binary).

r+

Opens a file for both reading and writing. The file pointer will be at the beginning of the file.

>>> t = open(“D:/derek.txt”, mode=’r+’)
>>> t.read()
‘We have a new ship\nMurek’
>>> t.write(“And a boat”)
10
>>>

We can write and read at the same time.

rb+

Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.

>>> t.read()
b’We have a new ship\r\nMurek’
>>> t.write(“And a boat”)
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
TypeError: ‘str’ does not support the buffer interface
>>>

w

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

>>> t = open(“D:/derek.txt”, mode=’w’)
>>> t.read()
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
io.UnsupportedOperation: not readable
>>> t.write(“And a boat”)
10
>>>

wb

Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

>>> t = open(“D:/derek.txt”, mode=’wb’)
>>> t.read()
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
io.UnsupportedOperation: read
>>> t.write(“And a boat”)
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
TypeError: ‘str’ does not support the buffer interface
>>>

w+

Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

>>> t = open(“D:/derek.txt”, mode=’w+’)
>>> t.read()

>>> t.write(“And a boat”)
10
>>>

wb+

Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

>>> t = open(“D:/derek.txt”, mode=’wb+’)
>>> t.read()
b’And a boat’
>>> t.write(“And something else”)
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
TypeError: ‘str’ does not support the buffer interface
>>>

a

Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

>>> t = open(“D:/derek.txt”, mode=’a’)
>>> t.read()
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
io.UnsupportedOperation: not readable
>>> t.write(“And something else”)
18
>>>

ab

Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

>>> t = open(“D:/derek.txt”, mode=’ab’)
>>> t.read()
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
io.UnsupportedOperation: read
>>> t.write(“And something else”)
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
TypeError: ‘str’ does not support the buffer interface
>>>

a+

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

>>> t = open(“D:/derek.txt”, mode=’a+’)
>>> t.read()

>>> t.write(“And something else”)
18
>>>

ab+

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

>>> t = open(“D:/derek.txt”, mode=’ab+’)
>>> t.read()
b”
>>> t.write(“And something else”)
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
TypeError: ‘str’ does not support the buffer interface
>>>

We have the exception:

TypeError: ‘str’ does not support the buffer interface

We  should convert our text to raw bytes in Python 3.x.x. We use bytes to do that:

bytes(“string”, “encoding”)

bytes(“We have bytes”, “utf-8”)

So let’s try to use it in the opening of our file:

>>> t = open(“D:/derek.txt”, mode=’ab+’)
>>> t.read()
b”
>>> t.write(bytes(“We have bytes”, “utf-8”))
13
>>>

It works!

Leave a comment