Hit enter after type your search item

Python open() function to perform read/write operations in files

In order to perform input/output (I/O) operations in files, you have to open a file. The Python open() function is used to open the specified file where you may perform reading, writing and other operations.

In this tutorial, I will show you how to open a file, read the content of a file, write to file and other related things related to file operations.

How to open files in Python?

The syntax of using the open() function is:

open(file, mode=’r’, buffering=-1, encoding=None, errors=None, newline=None)

The parameters of the open function are explained below:

  • file – The file parameter specifies the relative path to the current working directory or absolute path of the file that you want to open and perform some operation. If file cannot be opened an error occurs (OSError).
  • mode – This is where you will specify the purpose of opening a file. For example, ‘r’ (the default) value opens a file in reading mode for text files. If you require writing to a text file, then use ‘w’ mode. For opening a file in write mode for appending text, use the ‘a’ value. The detailed list of modes is given in the last section.
  • buffering – Set the buffering off or on by using an Integer value. The value 1 is text-mode line buffering. For switching off the buffering in binary mode, use the 0 value. This is an optional parameter. So, if not provided, the default buffering policy is applied.
  • newline – This parameter applies to text-mode only and used to specify the universal newlines. The following values can be used: \n, \r, \r\n, None and ‘’.

An example of reading a file by read method of open() function

In this example, a text file is opened in read-only mode by using the ‘r’ value for the mode parameter in Python open() file function. The text of the readme.txt file is displayed on the screen. The readme.txt file is placed at the same location where Python source file is placed:

See online demo and code

Python open

The following things should be noted in the program:

  • First of all, the file object is created and open() function is used.
  • In the open() function, the relative path is given for readme.txt file.
  • The mode parameter is ‘r’, that means open the file in read-only mode.
  • The file content is read by using the Python read() method of open() function.
  • The print function displayed the content of the file.
  • Finally, the close() function is used for closing the file.

The example of write to file by write() method

The following example shows using the Python write() method after creating a file object. In the open() function, the ‘w’ parameter is given for the mode argument. This enables us writing in a text file, however, the text file will be truncated as using this value. That means, existing text will be removed.

For demonstrating that, the content in the “readme.txt” file initially is “Hello World Python”. This is displayed before opening the file in read-only mode.

After that, the file is opened again in write-only mode and write() method is used for adding new text. The file is closed again and reopened in read-only mode for displaying the content:

See online demo and code

Python write

You can see, the initial text is truncated as we opened the file in write-only mode. The second print function displays only the new written text.

Using the append mode example

If you require keeping the initial text and avoid truncating this while writing to file then you may use the file append mode. For that, use the ‘a’ value for the mode parameter. This will append the new text to the end of existing content.

See this example where a few changes are made in above example. The file text before and after writing the new text is displayed and you can see the difference.

See online demo and code

append text open

You can see, the new content is added ahead of the existing text. The newline ‘\n’ is used for writing text in the next line.

Reading only limited characters in text file example

In all the above examples, the complete content of the text file is read and displayed by using the open() function’s read() method. If you require only a part of the text from the file then you may specify the size in the read() method as follows:

The output:

Reading limited data:  Python Hel


By default, the entire content is read and returned (if size is left as in the above examples).

Also, note that the returned data is either string for the text files and bytes object for binary mode.

Python read line by line example

You may use a for loop to loop over a file object for reading line by line. Have a look at this example where I wrote a few lines of text in the readme.txt file:

The output:

Line 1- Python is cool

Line 2- Python is Great

Line 3- Python is Awesome

Line 4- Love it!!!

This way of reading line by line is memory efficient and fast.

An example of readline() method to read line by line

The readline() method can also be used for reading a single line from the file. In that case, the new line character is left at the end of the string. The new line is omitted at the last line.

The Output:


Reading all lines in a list example

You may also read all lines in a file to the list. See the example below:

The output:

[‘Python\n’, ‘Read\n’, ‘Write\n’, ‘Read\n’, ‘Open\n’, ‘Close\n’, ‘\n’]

List of modes

  • ‘r’ – is the default that is used to open a file in read-mode only.
  • ‘r+’ opens the file in read and write mode.
  • ‘w’ A text file is opened in write-only mode. Truncates the content of the text file.
  • ‘rb’ – opens the file in binary format – read-only.
  • ‘rb+’ – binary mode. Read and write.
  • ‘wb’ – binary mode – write only.
  • ‘a’ – It creates a new file if it does not exist. Used to append at the end of the file. So existing contents remain in place.
  • ‘a+’ – it will open the file for read and appending.

Useful related reading: The with keyword as doing I/O operations in files.

This div height required for enabling the sticky sidebar