Reference Materials
Created with over a decade of experience.
Certification Courses
Created with over a decade of experience and thousands of feedback.
Built-in Functions
- Python abs()
- Python any()
- Python all()
- Python ascii()
- Python bin()
- Python bool()
- Python bytearray()
- Python callable()
- Python bytes()
- Python chr()
- Python compile()
- Python classmethod()
- Python complex()
- Python delattr()
- Python dict()
- Python dir()
- Python divmod()
- Python enumerate()
- Python staticmethod()
- Python filter()
- Python eval()
- Python float()
- Python format()
- Python frozenset()
- Python getattr()
- Python globals()
- Python exec()
- Python hasattr()
- Python help()
- Python hex()
- Python hash()
- Python input()
- Python id()
- Python isinstance()
- Python int()
- Python issubclass()
- Python iter()
- Python list() Function
- Python locals()
- Python len()
- Python max()
- Python min()
- Python map()
- Python next()
- Python memoryview()
- Python object()
- Python oct()
- Python ord()
- Python open()
- Python pow()
- Python print()
- Python property()
- Python range()
- Python repr()
- Python reversed()
- Python round()
- Python set()
- Python setattr()
- Python slice()
- Python sorted()
- Python str()
- Python sum()
- Python tuple() Function
- Python type()
- Python vars()
- Python zip()
- Python __import__()
- Python super()
Python open()
The syntax of open() is:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
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:Mode Description '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
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.
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.
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')
Also Read:
Did you find this article helpful?
