Menu Close

Python open Function

python open function

In this article, we are going to learn Python’s most important function which is an open() function. open() function in python is used to open a file and returns an object.

You can learn all about Python’s open() function in our file-handling tutorial.

Python open() function Introduction

The Python open function is a Python built-in function that means you don’t need to install it by using the Python pip command or you don’t need to import it from a module or package. Python open() function is used to open the file and return the file object. Using file objects you can perform various operations on the file.

Syntax

The syntax of Python open function is:-

open(filename, mode)

Return Value

The return value of the open() function in Python is a file object.

open() Parameters

open function in Python supports two parameters.

  1. filename:- The path and name of the file.
  2. mode:- A string, Define in which you want to open a file.

There are various types of modes available in Python open() function

  • “r”:- Read Mode, Open a file for reading purpose. This is the default mode of the open() function. It raises an error if the file does not exist
  • “w”:- Write Mode, Open a file for writing. Create a file if does not exist.
  • “a”:- Append Mode, Open a file for append. Create a file if the file does not exist.
  • “x”:- Create Mode, Create the specified file. Return an error if the specified file already exists.

You can use these two modes If you are working with binary files

  • “t”:- Text Mode, This mode is used to handle files in text mode. By default open() function used text mode.
  • “b”:- Binary Mode, This model will be used if the user wants to open a file in binary mode. This mode is generally used to handle image files.

Python open() function example

Let’s see some examples of Python open() function for different-different purposes. You can use the open() function to read the contents of the file, append content to the file, write some contents of the file, etc.

Example: Opening a file to read the contents

For demonstration, I have created a text file demo.txt having some contents. As you can see below.

demo.txt

Welcome to Programming Funda online Python tutorial.

Now will read content from the demo.txt using Python open function. To open a file for read mode, we will use “r” mode.


# opening file in read mode
file = open("demo.txt", "r")

# read content of the file
print(file.read())

# close the file
file.close()

Output

Welcome to Programming Funda online Python tutorial.

Example: Opening a file for writing contents

In this example, I am opening a file for writing some content. To open a file in write mode we have to use “w” mode. You have to remember one thing “w” will create a file If the specified file does not exist.


file = open('demo.txt', 'w')
file.write("This is my file")
file.close()

Example: Opening a file for append contents

In this example, I am going to append some contents to the existing file demo.txt. To append some content I will use “a” mode.


# opening file for append
file = open("demo.txt", "a")

# append content to the file
file.write("Hi, this is for append...")

# close the file
file.close()

Output

Welcome to Programming Funda online Python tutorial.Hi this is for append...

Example: Creating a file using the open() function

In this example, I will create only a file named test.txt using “x” mode.


# creating a file
file = open("test.txt", "x")

# close the file
file.close()

Conclusion

So, In this tutorial, you have learned about Python’s open functions along with examples. Python open() is the best function to work with file handling like opening a file, creating a file, appending contents to the file, creating a file, etc. You can explore more about the Python open() function in the following tutorials.

if you like this article, please share and keep visiting for further Python built-in functions tutorials.

Python built-in functions tutorials


For more information:- Click Here

Python print() Function
Python next() Function

Related Posts