open()
The built-in open()
function in Python is used to open a file and return a corresponding file object. This function allows you to read from or write to files, with various options for file modes and encoding. Here’s a quick example of how to use the function:
>>> with open("example.txt", mode="w") as file:
... file.write("Hello, World!")
...
13
open()
Signature
open(
file,
mode="r",
buffering=-1,
encoding=None,
errors=None,
newline=None,
closefd=True,
opener=None
)
Arguments
Argument | Description | Default Value |
---|---|---|
file |
A path-like object giving the pathname of the file to be opened. | Required |
mode |
A string specifying the mode in which to open the file (e.g., 'r' , 'w' , 'b' , etc.). |
'r' |
buffering |
An integer used to set the buffering policy. | -1 |
encoding |
The name of the encoding used to decode or encode the file. | None |
errors |
A string that specifies how encoding and decoding errors are to be handled. | None |
newline |
A string that determines how to parse newline characters from the stream. | None |
closefd |
A Boolean value that defines whether to close a file descriptor. | True |
opener |
A callable used as a custom opener for the target file. | None |
Return Value
- Returns a file object. The type of file object depends on the mode:
- Text mode returns a
TextIOWrapper
. - Binary mode returns a
BufferedReader
,BufferedWriter
,BufferedRandom
, orFileIO
.
open()
Examples
With a text file opened for writing:
>>> with open("example.txt", "w") as file:
... file.write("Hello, World!")
...
13
With a text file opened for reading:
>>> with open("example.txt", "r") as file:
... print(file.read())
...
Hello, World!
Using a specific encoding:
>>> with open("example.txt", "w", encoding="utf-8") as file:
... file.write("Hello, Pythonista!")
...
17
open()
Common Use Cases
The most common use cases for the open()
function include:
- Reading from and writing to text files.
- Handling binary files, such as images or executable files.
- Managing file encodings for internationalization.
- Implementing custom file handling logic with custom openers.
open()
Real-World Example
Let’s consider a scenario where you need to read a list of names from a file and write them to another file in uppercase. This can be achieved using the open()
function:
>>> with open("names.txt", "r") as input_file:
... names = input_file.readlines()
...
>>> with open("uppercase_names.txt", "w") as output_file:
... for name in names:
... output_file.write(name.upper())
...
In this example, the open()
function is used twice—first to read the names and then to write them in uppercase to a new file. This demonstrates how open()
facilitates file manipulation in Python.
Related Resources
Tutorial
Reading and Writing Files in Python (Guide)
In this tutorial, you'll learn about reading and writing files in Python. You'll cover everything from what a file is made up of to which libraries can help you along that way. You'll also take a look at some basic scenarios of file usage as well as some advanced techniques.
For additional information on related topics, take a look at the following resources:
- Context Managers and Python's with Statement (Tutorial)
- Why Is It Important to Close Files in Python? (Tutorial)
- Reading and Writing Files in Python (Course)
- Reading and Writing Files in Python (Quiz)
- Context Managers and Using Python's with Statement (Course)
- Context Managers and Python's with Statement (Quiz)