Open In App

File Handling in Python

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

File handling refers to the process of performing operations on a file, such as creating, opening, reading, writing and closing it through a programming interface. It involves managing the data flow between the program and the file system on the storage device, ensuring that data is handled safely and efficiently.

Why do we need File Handling

  • To store data permanently, even after the program ends.
  • To access external files like .txt, .csv, .json, etc.
  • To process large files efficiently without using much memory.
  • To automate tasks like reading configs or saving outputs.
  • To handle input/output in real-world applications and tools.

Opening a File in Python

To open a file, we can use open() function, which requires file-path and mode as arguments:

Syntax:

file = open('filename.txt', 'mode')

  • filename.txt: name (or path) of the file to be opened.
  • mode: mode in which you want to open the file (read, write, append, etc.).

Note: If you don’t specify the mode, Python uses 'r' (read mode) by default.

Basic Example: Opening a File

Python
f = open("geek.txt", "r")
print(f)

This code opens the file demo.txt in read mode. If the file exists, it connects successfully, otherwise, it throws an error.

Closing a File

It's important to close the file after you're done using it. file.close() method closes the file and releases the system resources ensuring that changes are saved properly (in case of writing)

Python
file = open("geeks.txt", "r")
# Perform file operations
file.close()

Checking File Properties

Once the file is open, we can check some of its properties:

Python
f = open("geek.txt", "r")

print("Filename:", f.name)
print("Mode:", f.mode)
print("Is Closed?", f.closed)

f.close()
print("Is Closed?", f.closed)

Output:

Filename: demo.txt
Mode: r
Is Closed? False
Is Closed? True

Explanation:

  • f.name: Returns the name of the file that was opened (in this case, "demo.txt").
  • f.mode: Tells us the mode in which the file was opened. Here, it’s 'r' which means read mode.
  • f.closed: Returns a boolean value- Fasle when file is currently open otherwise True.

Reading a File

Reading a file can be achieved by file.read() which reads the entire content of the file. After reading the file we can close the file using file.close() which closes the file after reading it, which is necessary to free up system resources.

Example: Reading a File in Read Mode (r)

Python
file = open("geeks.txt", "r")
content = file.read()
print(content)
file.close()

Output:

Hello world
GeeksforGeeks
123 456

Using with Statement

Instead of manually opening and closing the file, you can use the with statement, which automatically handles closing. This reduces the risk of file corruption and resource leakage.

Example: Let's assume we have a file named geeks.txt that contains text "Hello, World!".

Python
with open("geeks.txt", "r") as file:
    content = file.read()
    print(content)

Output:

Hello, World!

Handling Exceptions When Closing a File

It's important to handle exceptions to ensure that files are closed properly, even if an error occurs during file operations.

Python
try:
    file = open("geeks.txt", "r")
    content = file.read()
    print(content)
finally:
    file.close()

Output:

Hello, World!

Explanation:

  • try: Starts the block to handle code that might raise an error.
  • open(): Opens the file in read mode.
  • read(): Reads the content of the file.
  • finally: Ensures the code inside it runs no matter what.
  • close(): Safely closes the file to free resources.

Related articles: Modes in File Handling


File Handling in Python
Next Article
Article Tags :
Practice Tags :

Similar Reads