File handling in Python is essential for reading from and writing to files.
- Opening a File:
- Use the
open()
function to interact with files, specifying the file name and the mode ("r" for read, "w" for write, "a" for append, etc.).
- Use the
- Reading from a File:
- To read the contents of a file, use the
read()
method on the file object.
- To read the contents of a file, use the
- Writing to a File:
- To write content to a file, use the
write()
method on the file object.
- To write content to a file, use the
Example of reading from a file:
# Open a file for reading
file = open("example.txt", "r")
# Read the contents of the file
content = file.read()
print(content)
# Close the file
file.close()
Example of writing to a file:
# Open a file for writing
file = open("example.txt", "w")
# Write content to the file
file.write("Hello, Python!")
# Close the file
file.close()
Understanding file modes and using context managers (with statement) can simplify file handling.
- File Modes:
- File modes, such as "r" (read), "w" (write), and "a" (append), determine how the file is opened.
- Context Managers:
- Context managers ensure that files are automatically closed, even if an exception occurs.
Example of using a context manager to read a file:
# Using a context manager to automatically close the file
with open("example.txt", "r") as file:
content = file.read()
print(content)
Error handling allows you to gracefully handle exceptions and errors in your code.
- Try-Except Blocks:
- Use
try
andexcept
blocks to catch and handle exceptions.
- Use
- Exception Types:
- Python has many built-in exception types, and you can create custom exceptions.
Example of handling an exception:
try:
num = int("abc") # This will raise a ValueError
except ValueError as e:
print(f"An error occurred: {e}")
Handling multiple exceptions and creating custom exceptions enhance your error-handling capabilities.
- Handling Multiple Exceptions:
- You can use multiple
except
blocks to handle different exception types.
- You can use multiple
- Custom Exceptions:
- Create custom exception classes by defining new exception types.
Example of handling multiple exceptions and creating a custom exception:
try:
result = 10 / 0 # This will raise a ZeroDivisionError
except ZeroDivisionError as e:
print(f"Division by zero error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# Custom exception
class MyCustomError(Exception):
pass
try:
raise MyCustomError("This is a custom error.")
except MyCustomError as e:
print(f"Custom error caught: {e}")
Mastering file handling and error handling is crucial for writing robust and reliable Python programs. These skills enable you to work with files effectively and gracefully manage errors that may occur during program execution. Practice with these examples to become proficient in file handling and error handling in Python.