You are here : python_3Built-in Functionsopen

open() - Built-in Functions

Open file and return a corresponding file object.  If the file
cannot be opened, an OSError is raised.


Syntax

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)


Example

>>> import os
>>> dir_fd = os.open('somedir', os.O_RDONLY)
>>> def opener(path, flags):
...     return os.open(path, flags, dir_fd=dir_fd)
...
>>> with open('spamspam.txt', 'w', opener=opener) as f:
...     print('This will be written to somedir/spamspam.txt', file=f)
...
>>> os.close(dir_fd)  # don't leak a file descriptor


Output / Return Value


Limitations


Alternatives / See Also


Reference