File Handling & Exception Handling in Python: Complete Guide for Beginners
Introduction
In Python, working with files and handling errors are two essential skills every developer must learn. Whether you’re reading data from files or writing logs, things can go wrong — and that’s where exception handling in Python becomes crucial.
In this blog, we’ll explore file handling, understand common file handling exceptions in Python, and learn how to handle them effectively.
What is File Handling in Python?
File handling allows you to work with files such as .txt, .csv, or .log files. Python provides built-in functions to create, read, write, and delete files.
Basic File Operations:
content = file.read() # Read file
file.close() # Close file
File Modes:
"r"→ Read"w"→ Write"a"→ Append"x"→ Create
Why Exception Handling is Important
While working with files, errors can occur, such as:
- File not found
- Permission issues
- Incorrect file format
This is where exception handling in Python helps prevent program crashes and ensures smooth execution.
What is Exception Handling in Python?
Exception handling is a way to manage runtime errors using try, except, else, and finally.
Basic Syntax:
try: file = open("data.txt", "r") print(file.read()) except FileNotFoundError: print("File not found!") finally: print("Execution completed")
File Handling Exceptions in Python
When dealing with files, several common exceptions may occur:
FileNotFoundError
Raised when the file does not exist.
try: open("missing.txt", "r") except FileNotFoundError: print("File does not exist")
PermissionError
Occurs when you don’t have permission to access a file.
try: open("protected.txt", "r") except PermissionError: print("Permission denied")
IOError
General input/output error.
try: open("file.txt", "r") except IOError: print("File cannot be opened")
Best Practice: Using with Statement
Instead of manually closing files, use with — it automatically handles closing.
try: with open("data.txt", "r") as file: print(file.read()) except Exception as e: print("Error:", e)
This is the safest way for file handling exceptions in Python
Combining File Handling & Exception Handling
Here’s a complete example:
try: with open("data.txt", "r") as file: content = file.read() print(content) except FileNotFoundError: print("File not found") except PermissionError: print("No permission to read file") except Exception as e: print("Something went wrong:", e) finally: print("Done")
Conclusion
Understanding file handling along with exception handling in Python is essential for building robust applications. Properly managing file handling exceptions in Python ensures your programs run smoothly without unexpected crashes.
Master these concepts, and you’ll write cleaner, safer, and more professional Python code.
“Are you looking to build a career in google Data Analytics?
Learnomate Technologies brings you a complete Data Analyst course designed for beginners and professionals.
Learn SQL, Excel, Power BI, and Python with real-time projects and expert guidance.
Get job-ready with hands-on training and placement support.
Enroll today and take your first step towards becoming a Data Analyst!”





