Python – open()
Python open() built-in function is used to open a file given by specific path, and in specified mode.
In this tutorial, we will learn the syntax and usage of open() built-in function with examples.
Heads up! There are many parameters for this function. But, do not worry. We use only one or two of these in typical programs. And we shall explain these in examples. But for completeness of this builtin function tutorial, we are going to mention all of the parameters, so that you would know what all this open() function can do.
The syntax of open() function is
open(file, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
|Mandatory||This is a path-like object giving the pathname to the file. This can be absolute path or relative path to the current working directory.|
|Optional||The mode in which the file has to be opened. A separate table for this parameter is given below.|
|Optional||Specifies the buffering policy.|
|Optional||The name of encoding used to encode or decode the file.|
|Optional||Specifies how encoding and decoding errors are to be handled.|
|Optional||Specifies how newline characters from the file stream are to be handled.|
|Optional||A boolean value specifying a file descriptor for closing the file.|
|Optional||A callable function can be passed for this parameter that returns an open file descriptor.|
mode can take any of the following character. The meaning of each character is also given.
|Opens file in reading mode. You cannot edit file in this mode.|
|Open file in writing mode. If there is any content in the file, the content is truncated.|
|Open file with the exclusive operation for creating the file. If the file already exists, then open() function fails.|
|Opens file in writing mode. If the file already exists, then it appends the new content to the end of file.|
|Opens file in binary mode, where you can read the bytes of the file.|
|Opens the file in text mode. This is the default mode for open() function.|
|Opens file for updating. You can both read from the file, and write to the file.|
Some of these options can be combined, as shown in the following.
|Opens file in write mode, and truncates the file if there is any content.|
|Opens the file, and does not truncate the file if there is any content.|
|Same as |
In the following examples, we will go through programs using open() function to open a file in default mode, write mode, append mode, etc.
1. Open file in default mode (text)
In the following program, we will open a text file given by the relative path
filepath = "data/sample.txt" myFile = open(filepath) print(myFile)
<_io.TextIOWrapper name='data/sample.txt' mode='r' encoding='UTF-8'>
We just printed out the file object. To display the content of the file, call read() function on the file object.
filepath = "data/sample.txt" myFile = open(filepath) print(myFile.read()) myFile.close()
Yeah ‘Hello World’ is our file content.
2. Open file in write mode, and override content
In the following program, we will open the text file in write mode, and override the content with a string value
'Welcome to new world!'.
filepath = "data/sample.txt" myFile = open(filepath, 'w') myFile.write('Welcome to new world!') myFile.close()
3. Append content to file
In the following program, we will open the text file in append mode, and append the content with a string value
' apple banana cherry'.
filepath = "data/sample.txt" myFile = open(filepath, 'a') myFile.write(' apple banana mango') myFile.close()
In this Built-in Functions tutorial, we learned the syntax of the open() built-in function, and how to use this function to open a file in specific mode, and read or modify the file.