DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Originally published at flexiple.com

How to use Open() in Python?

You can open internal files in Python with the help of a simple function, open(). Here we will learn how to use this function in Python.

Table of contents

  1. Open() in Python
  2. Code and Explanation
  3. Closing thoughts

Open() in Python

The open() function in Python opens the internally stored files and returns the content of the corresponding file as a python object.

Syntax of Open() in Python:

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

Parameters:

  • file: It is the path name of the file that we want to open.
  • mode: It is a string that defines in which mode the file is opened, but If the mode is not provided, it takes up the default value of 'r'.
  • buffering: It is an optional parameter used to set buffering policy.
  • encoding: It is another optional parameter used to encode the format.
  • errors: It is also an optional parameter that specifies how to handle encoding and decoding errors.
  • newline: This strings tells how newlines mode works ( values: None, ' ', '\n', 'r', and '\r\n').
  • closefd: This string value must be TRUE (also the default value), otherwise an exception is raised.
  • opener: It is a custom opener and must return an open file descriptor.

The other strings that can be used are given as follow:

  • 'r': This string is used to read(only) the file. It is passed as default if no parameter is supplied and returns an error if no such file exists.
  • 'rb': This string is used to read(only) the file in binary format.
  • 'r+': This string is used to both read and write the file.
  • 'rb+': This string is used to both read and write the file in binary format.
  • 'w': This string is used for writing on/over the file. If the file with the supplied name doesn’t exist, it creates one for you.
  • 'w+': This string is used for both reading and writing on/over the file.
  • 'wb': This string is used for writing on/over the file in binary format.
  • 'wb+': This string is used for both reading and writing on/over the file in binary format.
  • 'a': This string is used to add(append) content to an existing file. If no such file exists, it creates one for you.
  • 'ab': This string is used to add(append) content to an existing file in binary format.
  • 'ab+': This string is used to add(append) and read content to an existing file in binary format.
  • 'a+': This string is used to add(append) and read content to an existing file.
  • 'x': This string is used to create a specific file.

  • 'b': This string is used when the user wants to handle the file in binary mode. This is generally used to handle image files.

  • 't': This string is used to handle files in text mode. By default, the open() function uses the text mode.

Code and Explanation

Remember that, the open() function returns an internal file that is used to read, write and add content to the file, but if the file you want to open, doesn't exist then it throws FileNotFoundError error.

Input:

Open file to read
file = open (file_path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
print(file.read() == False)

Open file to write
file = open (file_path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
print(file.read() == False)

Open file to append
file = open (file_path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
print(file.read() == False) 
Enter fullscreen mode Exit fullscreen mode

Output:

TRUE
TRUE
TRUE
Enter fullscreen mode Exit fullscreen mode

Closing thoughts

The open() in Python lets you open any internal file if it exists which can be read, written or appended according to the user's need. The different strings value in mode lets you perform those functions as per your need. One can learn about more Python concepts here.

Top comments (0)