×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python File open() Method with Example

Python File open() Method: Here, we are going to learn about the open() method, how to create, open or append a file in various modes in Python. By IncludeHelp Last updated : November 26, 2023

File open() Method

The open() method is an inbuilt method in Python, it is used to create, open or append a file.

Syntax

The syntax of the open() method is:

file_object = open(file_name, file_mode)

Parameter(s)

The following are the parameter(s) of the open() method:

  • file_name – It is used to specify the file name.
  • file_mode – It is an optional parameter, it is used to specify the various file modes.
    • w – Opens the file in write mode i.e. creates a file.
    • r – Opens the file in reading mode.
    • a – Opens the file in append mode.
    • x – Creates the file, if file exists it returns an error.
    • t – It is used to file modes to specify the text mode (Example: wt, rt, at, and xt).
    • b – It is used to file modes to specify the binary mode (Example: wb, rb, ab, and xb).

Return value

The return type of this method is <class '_io.TextIOWrapper'>, it returns a file object.

open() Method: Example 1

# Python File open() Method with Example

print("creating files...")
# creating a file without specifying mode (b or t)
file1 = open("hello_1.txt", "w")

# creating a file in binary mode
file2 = open("hello_2.txt", "wb")

# creating a file in text mode
file3 = open("hello_3.txt", "wt")

print("file creation operation done...")

# printing the details of file objects
print(file1)
print(file2)
print(file3)

Output

creating files...
file creation operation done...
<_io.TextIOWrapper name='hello_1.txt' mode='w' encoding='UTF-8'>
<_io.BufferedWriter name='hello_2.txt'>
<_io.TextIOWrapper name='hello_3.txt' mode='wt' encoding='UTF-8'>

open() Method: Example 2

# Python File open() Method with Example

# creating a file
f = open("hello.txt", "w")
print("file created...")
print(f) # prints file details

# opening created file in read mode
f = open("hello.txt", "r")
print("file opened...")
print(f) # prints file details

# opening file in append mode 
f = open("hello.txt", "a")
print("file opened in append mode...")
print(f) # prints file details

Output

file created...
<_io.TextIOWrapper name='hello.txt' mode='w' encoding='UTF-8'>
file opened...
<_io.TextIOWrapper name='hello.txt' mode='r' encoding='UTF-8'>
file opened in append mode...
<_io.TextIOWrapper name='hello.txt' mode='a' encoding='UTF-8'>

open() Method: Example 3

# Python File open() Method with Example

# opening a file that doesn't exist
f = open("myfile.txt") # returns an error

Output

Traceback (most recent call last):
  File "main.py", line 4, in <module>
   f = open("myfile.txt") # returns an error
FileNotFoundError: [Errno 2] No such file or directory: 'myfile.txt'   
Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.