Opening and Closing Files in Python



Until now, you have been reading and writing to the standard input and output. Now, we will see how to interact with actual data files. Python provides built-in functions and methods necessary to manipulate files by default. To work with a file in Python, we first need to create a file object. Using this object you can do most of the file manipulation such as reading, writing, or closing a file.

Opening a File in Python

Before you can read or write a file, you have to open it using Python's built-in open() function. This function creates a file object, which would be utilized to call other supporting methods like read, write, close, and more.

Syntax

Following is the syntax of the Python open() method ?

open(file_name, mode)

Here are parameter details ?

  • file: This parameter takes a string value that contains the name of the file that you want to access.

  • mode: It determines the mode in which the file has to be opened, i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is optional parameter and the default file access mode is read (r).

  • buffering: This parameter takes a string value that contains the name of the file that you want to access.

Here is a list of the different modes of opening a file ?

Sr.No Modes & Description
1 r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
2 rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
3 r+ Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
4 rb+ Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.
5 w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
6 wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
7 w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
8 wb+ Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
9 a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
10 ab Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
11 a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
12 ab+ Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

The File Object Attributes

After creating a file object using the open() function, you can get various information related to that file using the attributes this object. Below we can see the list of all attributes related to file object ?

Sr.No Modes & Description
1 file.closed It returns True if file is closed, False otherwise.
2 file.mode Returns access mode with which file was opened.
3 file.name Returns name of the file.
4 file.softspace It returns False if space explicitly required with print, True otherwise.

Example

The following example shows how to open a file in Python using the open() function with the default read mode. Then we will retrieve file information using the attributes of the file.

#Open a file 
fo = open("foo.txt", "wb") 
print("Name of the file: ", fo.name)
print("Closed or not : ", fo.closed )
print("Opening mode : ", fo.mode) 

This produces the following result ?

Name of the file:  foo.txt
Opening mode :  r
Closed or not :  False

Closing a File in Python

Python automatically closes a file when the reference object of a file is reassigned to another file. However, it is recommended to explicitly closes it by calling the file_object.close() method. The close() method of a file object flushes any unwritten information and closes the file object, after which no more writing can be done.

Syntax

Following is the syntax of the Python open() method ?

fileObject.close()

Example

This example demonstrates closing a file in Python using the close() function.

# Open a file 
fo = open("foo.txt", "wb") 
print("Name of the file: ", fo.name)

# Close the opened file 
fo.close()

# check if the file is closed or not
print('File closed:',fo.closed)

This produces the following result ?

Name of the file:  foo.txt
File closed: True

Opening and Closing a File using the With Statement

It is recommended to open a file using the with statement, as it is automatically closes the file after the block ends. If we open the file without using the with statement and forget to call f.close() method, then the data may not be completely written to the file.

Example

This example demonstrates opening a file in Python using the with statement.

# Open a file 
with open("foo.txt", "r") as fo:
    read_data = fo.read()

# check if the file is closed or not
print("File closed:",fo.closed)

This produces the following result ?

File closed: True
Updated on: 2025-09-01T11:39:37+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements