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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# The example of open() functio for reading a text file objFile = open("readme.txt", "r") fileContent = objFile.read(); print ("The text file contains: : ", fileContent) # After performing the operation, close the file objFile.close() |

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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# The example of open() functio for reading a text file objRead1 = open("readme.txt", "r") ContFile = objRead1.read(); print ("Content before write() : ", ContFile) # Closing the first instance objRead1.close() #Opening again for write to file objWrite = open("readme.txt", "w") objWrite.write ("Using 'w' for writing text in file") #Closing the file in write mode objWrite.close() #Openging the file again for displaying content after writing objRead2 = open("readme.txt", "r") ContAfterWrite = objRead2.read(); print ("The content after write() : ", ContAfterWrite) objRead2.close() |

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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# The example of append mode objRead1 = open("readme.txt", "r") ContFile = objRead1.read(); print ("Content before appending text : ", ContFile) # Closing the first instance objRead1.close() #Open file in append mode objAppend = open("readme.txt", "a") objAppend.write ("Please dont remove the text..\nJust add ahead of existing text! Thanks") #Closing the file in append mode objAppend.close() #Open and read after append(0 objRead2 = open("readme.txt", "r") ContAfterAppend = objRead2.read(); print ("The content after append() : ", ContAfterAppend) objRead2.close() |

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:
1 2 3 4 5 6 7 8 9 10 11 12 |
# Read file with size objFile = open("readme.txt", "r") fileContent = objFile.read(10); print ("Reading limited data: ", fileContent) objFile.close() |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Read file line by line objFile = open("readme.txt", "r") for linebyline in objFile: print(linebyline, end = '') objFile.close() |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Read file readline() objFile = open("readme.txt", "r") print (objFile.readline()); print (objFile.readline()); objFile.close() |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Read file in list example objFile = open("readme.txt", "r") a_list = list(objFile) print (a_list) objFile.close() |
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.