Preparing for Python Interview?
Explore our Most asked Python Interview Questions and Answers.
File handling in Python is a method of interacting with files on the local file system. It involves performing different operations like creating, opening, reading, writing, appending and closing them. It is important for storing and retrieving data persistently, which helps to manage configurations, handle logs and process large datasets.
Understanding this concept is very important for Python developers, data analysts, web developers and system administrators. Each of them deals with these files. I have designed this comprehensive guide to explain the complete process from opening and writing to closing and managing potential errors. Let’s begin:
File handling in Python is the process of working with files on secondary storage systems, like a hard drive. It uses built-in functions to perform operations such as creating, reading, updating and deleting files.
It is used to manage external data stored in files. It also helps Python programs to interact with the persistent data on your disk. This makes it possible to store the output of your program for later use or to read input data for processing.
Learning file handling is important because it moves your programs beyond temporary data storage. Here are some of the common reasons behind its importance:
| Reasons | Explanation |
| Data Persistence | - Variables are destroyed when a program terminates. - Files allow you to store data permanently. |
| External Configuration | - You can read configuration settings like API keys or database credentials from a file. |
| Log Files | - Programs create log files to record events, errors and system status for debugging and monitoring. |
| Data Processing | - You can read large datasets from CSV, JSON or text files for analysis. |
Also read: Python Tutorial
Build your Python programming foundation through practical training, coding exercises, and hands-on projects designed for real-world applications.
Python generally deals with two main types of files and both of them require a specific approach, including:
Text Files in Python store data in a sequence of characters. This data is mostly encoded in a format like ASCII or UTF-8 and is easy to read. You can find them in .txt, .py, .csv or .html format. It is also easy to work with text files, as the Python runtime automatically handles the encoding/decoding process.
Binary files in Python store data in a sequence of raw bytes. These are not easy to read and can be found in and include file types like images (.jpg, .png), video, compiled code and compressed data (.zip). Python passes this data without any encoding conversion while working with binary files.
Opening the files is the first operation one must do to manage them. Python's built-in function open() is used for this and returns a file object. It acts as the link between your program and the file. The syntax for the open() function is:
file_object = open(file_name, access_mode) |
The key to file handling is the access mode, which specifies the type of operation you intend to perform. The table given below entails these mods and their intent:
| Mode | Description | Action if File Exists | Action if File Does NOT Exist |
| 'r' | Read (Default) | Cursor placed at the beginning. | Raises a FileNotFoundError. |
| 'w' | Write | Overwrites the entire file. | Creates a new file. |
| 'a' | Append | Cursor placed at the end. | Creates a new file. |
| 'r+' | Read and Write | Cursor placed at the beginning. | Raises a FileNotFoundError. |
| 'w+' | Write and Read | Overwrites the entire file. | Creates a new file. |
| 'a+' | Append and Read | Cursor placed at the end. | Creates a new file. |
try:
file = open("my_data.txt", "r")
print("File opened successfully.")
except FileNotFoundError:
print("Error: The file does not exist.") |
Also explore related articles: Python MCQs
Explore data science and business intelligence training to develop practical skills in data analysis, visualization, Python, and business reporting.
You can write anything in a file once it is opened in a writing ('w', 'a', 'w+', 'a+') mode. Use the file object's write() method for it. The write() method takes a string as an argument and writes it to the file. It is important to remember that write() does not automatically add a newline character (\n). You must include it manually if you want content on separate lines.
# Open in 'w' mode (will create the file or overwrite existing content)
file = open("my_output.txt", "w")
# Write some content
file.write("First line of text.\n")
file.write("Second line, written in Python.")
# Close the file to ensure data is saved
file.close() |
If you ran this code again, the first line would overwrite the file and only the two lines above would remain.
# Open in 'a' mode (will add to the end of existing content)
file = open("my_output.txt", "a")
# Append a new line
file.write("\nThis line was appended later.")
file.close() |
It is the most important step in file handling. You must call the close() method on the file object to ensure all changes are saved and the file is released. Failing to close a file can lead to:
1. Data Loss/Corruption: Data written to the file might be temporarily buffered and not actually saved to disk until the file is closed.
2. Resource Leaks: The operating system has a limit on the number of files a program can have open simultaneously. Unclosed files consume system resources.
3. File Locking: Other programs might not be able to access or modify the file until your program closes it.
file = open("demo.txt", "w")
# ... operations ...
file.close() # Always remember to close! |
Wherever you explicitly call file.close(), it always shows an error. This error occurs between the open() and close() calls, which may crash the program before reaching the close() statement. The best way to manage file operations is using the with statement in conjunction with the open() function. The with statement uses context managers and guarantees that the close() method is automatically called.
with open("my_file.txt", "r") as file:
# Operations go inside this block
content = file.read()
# The file is automatically closed as soon as the block is exited
print(content)
print(file.closed) # Output: True |
The file object is assigned to the variable defined after the as keyword. Python handles the cleanup automatically once the indented block is finished. This makes your code safer and cleaner.
Closing a file explicitly can also lead to an exception that could prevent the code from reaching file.close(). The most robust way to manage this without the with statement is by using a try...finally block. The finally block is guaranteed to execute even if an exception occurred in the try block.
file = None
try:
# 1. Open the file
file = open("dangerous_operation.txt", "w")
# 2. Perform a potentially risky operation
file.write("Writing data...")
# Imagine a ZeroDivisionError happens here...
except Exception as e:
print(f"An error occurred: {e}")
finally:
# 3. Ensure the file is closed, regardless of errors
if file is not None:
file.close()
print("File successfully closed in finally block.")
|
Beyond reading and writing, the os and shutil modules provide powerful tools for file management:
| Module | Purpose | Key Function Example |
| os | Interacting with the operating system (paths, environment, etc.) | os.remove("old_file.txt") (Delete a file) |
| os.path | Checking and manipulating file paths | os.path.exists("file.txt") (Check if a path exists) |
| shutil | High-level file operations (copying, moving) | shutil.copy("src.txt", "dest.txt") (Copy a file) |
import os
if os.path.exists("my_output.txt"):
os.remove("my_output.txt")
print("File deleted successfully.")
else:
print("The file does not exist.") |
Learn Python programming and artificial intelligence through practical training, hands-on projects, and real-world applications to build skills for AI-driven careers.
The default access mode for the open() function is 'r' (read mode) for text files. Python attempts to open the file for reading after you omit the mode argument.
These are methods used on a file object opened in read mode:
Use the exclusive creation mode 'x'. This mode is specifically designed to create a new file.
File Handling in Python is a foundational concept that bridges your running program with the permanent data storage of your computer. By understanding the different access modes, utilizing the robust with statement for automatic cleanup, and managing your files with the os module, you can build powerful and reliable applications. Always default to the with open(...) construct. It is the best practice for secure and effective file management.
Learn Other Python Concepts with Our Comprehensive Guides:-