Python Open() function

Published February 16, 2022

The open() function is the Python built-in function that is used to open all the internally stored files. This function takes the name of the file and returns the file contents in the form of python objects.
Now let's take a look at the syntax of the open() function.

Syntax

open(file, mode)

Where:
File- this is the4 name of the file to be opened
Mode- this specifies the mode of the file to be opened. The mode includes: read-only, writing on/over, or append.


Example

In the example below, we will create a text file and read that file.

read_file = open("Python.txt", "w")
read_file.write("I Love Python")
read_file.close()
read_file = open("Python.txt","r")
print(read_file.read())


Output
When the open() function is called, Python.txt is opened.

 

Python open() function to open file

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

237 Views