Python Error Handling & File Handling
Python Error Handling & File Handling for Beginners
In this post, you will learn how Python:
-
Handles errors
-
Reads and writes files
-
Uses modules and packages
1. Python Errors & try-except
When something goes wrong, Python shows an error.Common Error Types
-
ValueError -
TypeError -
ZeroDivisionError -
IndexError -
KeyError
Full try-except Syntax
3. Python Modules & Packages
A module is a file with Python code.A package is a folder of modules.
| Module | Important Methods |
|---|---|
math | sqrt(), pow(), ceil(), floor() |
random | randint(), choice(), shuffle() |
datetime | now(), date() |
os | getcwd(), mkdir(), remove() |
Using a Module
Create Your Own Module
Create file: my_module.py
Use it:
Now you can:
-
Handle errors
-
Save data in files
-
Reuse code with modules
Comments
Post a Comment