Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 18, 2022 11:33 am GMT

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)

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, ' ', '
    ', 'r', and '\r
    ').
  • 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 doesnt 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 readfile = open (file_path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)print(file.read() == False)Open file to writefile = open (file_path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)print(file.read() == False)Open file to appendfile = open (file_path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)print(file.read() == False) 

Output:

TRUETRUETRUE

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.


Original Link: https://dev.to/hrishikesh1990/how-to-use-open-in-python-1pb3

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To