|
| 1 | +### **Day 15: File Handling** |
| 2 | +File handling in Python is essential for reading from and writing to files. |
| 3 | + |
| 4 | +- **Opening a File:** |
| 5 | + - 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.). |
| 6 | +- **Reading from a File:** |
| 7 | + - To read the contents of a file, use the `read()` method on the file object. |
| 8 | +- **Writing to a File:** |
| 9 | + - To write content to a file, use the `write()` method on the file object. |
| 10 | + |
| 11 | +**Example of reading from a file:** |
| 12 | +```python |
| 13 | +# Open a file for reading |
| 14 | +file = open("example.txt", "r") |
| 15 | + |
| 16 | +# Read the contents of the file |
| 17 | +content = file.read() |
| 18 | +print(content) |
| 19 | + |
| 20 | +# Close the file |
| 21 | +file.close() |
| 22 | +``` |
| 23 | + |
| 24 | +**Example of writing to a file:** |
| 25 | +```python |
| 26 | +# Open a file for writing |
| 27 | +file = open("example.txt", "w") |
| 28 | + |
| 29 | +# Write content to the file |
| 30 | +file.write("Hello, Python!") |
| 31 | + |
| 32 | +# Close the file |
| 33 | +file.close() |
| 34 | +``` |
| 35 | + |
| 36 | +### **Day 16: File Modes and Context Managers** |
| 37 | +Understanding file modes and using context managers (with statement) can simplify file handling. |
| 38 | + |
| 39 | +- **File Modes:** |
| 40 | + - File modes, such as "r" (read), "w" (write), and "a" (append), determine how the file is opened. |
| 41 | +- **Context Managers:** |
| 42 | + - Context managers ensure that files are automatically closed, even if an exception occurs. |
| 43 | + |
| 44 | +**Example of using a context manager to read a file:** |
| 45 | +```python |
| 46 | +# Using a context manager to automatically close the file |
| 47 | +with open("example.txt", "r") as file: |
| 48 | + content = file.read() |
| 49 | + print(content) |
| 50 | +``` |
| 51 | + |
| 52 | +### **Day 17: Error Handling (Try-Except Blocks)** |
| 53 | +Error handling allows you to gracefully handle exceptions and errors in your code. |
| 54 | + |
| 55 | +- **Try-Except Blocks:** |
| 56 | + - Use `try` and `except` blocks to catch and handle exceptions. |
| 57 | +- **Exception Types:** |
| 58 | + - Python has many built-in exception types, and you can create custom exceptions. |
| 59 | + |
| 60 | +**Example of handling an exception:** |
| 61 | +```python |
| 62 | +try: |
| 63 | + num = int("abc") # This will raise a ValueError |
| 64 | +except ValueError as e: |
| 65 | + print(f"An error occurred: {e}") |
| 66 | +``` |
| 67 | + |
| 68 | +### **Day 18: Error Handling (Multiple Exceptions and Custom Exceptions)** |
| 69 | +Handling multiple exceptions and creating custom exceptions enhance your error-handling capabilities. |
| 70 | + |
| 71 | +- **Handling Multiple Exceptions:** |
| 72 | + - You can use multiple `except` blocks to handle different exception types. |
| 73 | +- **Custom Exceptions:** |
| 74 | + - Create custom exception classes by defining new exception types. |
| 75 | + |
| 76 | +**Example of handling multiple exceptions and creating a custom exception:** |
| 77 | +```python |
| 78 | +try: |
| 79 | + result = 10 / 0 # This will raise a ZeroDivisionError |
| 80 | +except ZeroDivisionError as e: |
| 81 | + print(f"Division by zero error: {e}") |
| 82 | +except Exception as e: |
| 83 | + print(f"An unexpected error occurred: {e}") |
| 84 | + |
| 85 | +# Custom exception |
| 86 | +class MyCustomError(Exception): |
| 87 | + pass |
| 88 | + |
| 89 | +try: |
| 90 | + raise MyCustomError("This is a custom error.") |
| 91 | +except MyCustomError as e: |
| 92 | + print(f"Custom error caught: {e}") |
| 93 | +``` |
| 94 | + |
| 95 | +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. |
0 commit comments