Home » Python

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?
Submitted by IncludeHelp, on December 16, 2019

File open() Method

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

Syntax:

    file_object = open(file_name, file_mode)

Parameter(s):

  • 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.

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'>

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'>

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'   





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.