Table of Contents
The open() function opens the file (if possible) and returns the corresponding file object.
The syntax of open()
is:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
1. open() Parameters
- file – path-like object (representing a file system path)
- mode (optional) – mode while opening a file. If not provided, it defaults to
'r'
(open for reading in text mode). Available file modes are:ModeDescription'r'
Open a file for reading. (default)'w'
Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists.'x'
Open a file for exclusive creation. If the file already exists, the operation fails.'a'
Open for appending at the end of the file without truncating it. Creates a new file if it does not exist.'t'
Open in text mode. (default)'b'
Open in binary mode.'+'
Open a file for updating (reading and writing) - buffering (optional) – used for setting buffering policy
- encoding (optional) – the encoding format
- errors (optional) – string specifying how to handle encoding/decoding errors
- newline (optional) – how newlines mode works (available values:
None
,' '
,'\n'
,'r'
, and'\r\n'
- closefd (optional) – must be
True
(default); if given otherwise, an exception will be raised - opener (optional) – a custom opener; must return an open file descriptor
2. Return Value from open()
The open()
function returns a file object which can used to read, write and modify the file.
If the file is not found, it raises the FileNotFoundError
exception.
3. Example 1: How to open a file in Python?
# opens test.text file of the current directory f = open("test.txt") # specifying the full path f = open("C:/Python33/README.txt")
Since the mode is omitted, the file is opened in 'r'
mode; opens for reading.
4. Example 2: Providing mode to open()
# opens the file in reading mode f = open("path_to_file", mode='r') # opens the file in writing mode f = open("path_to_file", mode = 'w') # opens for writing to the end f = open("path_to_file", mode = 'a')
Python’s default encoding is ASCII. You can easily change it by passing the encoding
parameter.
f = open("path_to_file", mode = 'r', encoding='utf-8')
Recommended Reading: Python File Input/Output
Related posts:
Python Numbers, Type Conversion and Mathematics
Python String lower()
Python List pop()
Python Iterators
Python Shallow Copy and Deep Copy
Python List reverse()
Python zip()
Node.js vs Python for Backend Development
Python String rfind()
Python String title()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python dir()
Python Set difference()
Python Program to Find Numbers Divisible by Another Number
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Namespace and Scope
Python String expandtabs()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Return Multiple Values From a Function
Python Program to Append to a File
Deep Learning in Python - LazyProgrammer
Python Data Types
Python Global, Local and Nonlocal variables
Python File I/O Operation
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python object()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python strftime()
Python Program to Get a Substring of a String
Python String find()