Python Tools and Utilities
Python is not only a programming language but also a powerful toolbox.
It comes with many built-in tools and utility modules that help developers:
- Work with files and folders
- Manage system operations
- Handle dates and time
- Run system commands
- Log application activities
- Automate tasks
These utilities make Python ideal for scripting, automation, and system programming.
Why Python Tools/Utilities Matter
Python utilities help you:
- Automate repetitive tasks
- Manage system resources
- Build admin tools
- Create automation scripts
- Improve productivity
They are widely used in DevOps, backend systems, and data engineering.
1. OS Module (Operating System Operations)
The os module helps interact with the operating system.
Example: Get Current Directory
import os
print(os.getcwd())List Files in Directory
print(os.listdir())Create Folder
os.mkdir("new_folder")2. SYS Module (System Functions)
The sys module provides access to system-level parameters.
Example
import sys
print(sys.version)
print(sys.platform)Command-line Arguments
print(sys.argv)3. PATHLIB Module (Modern File Handling)
pathlib provides an object-oriented way to handle file paths.
Example
from pathlib import Path
path = Path("example.txt")
print(path.exists())Create File
path.write_text("Hello Python")4. SHUTIL Module (File Operations)
The shutil module helps with file copying and moving.
Copy File
import shutil
shutil.copy("source.txt", "destination.txt")Move File
shutil.move("file.txt", "folder/")5. DATETIME Module (Working with Dates)
Get Current Date and Time
import datetime
print(datetime.datetime.now())Format Date
now = datetime.datetime.now()
print(now.strftime("%Y-%m-%d"))6. LOGGING Module (Application Logs)
The logging module helps track application events.
Basic Logging
import logging
logging.basicConfig(level=logging.INFO)
logging.info("Application started")Logging Levels
| Level | Description |
|---|---|
| DEBUG | Detailed info |
| INFO | General events |
| WARNING | Warning messages |
| ERROR | Errors |
| CRITICAL | Severe issues |
7. SUBPROCESS Module (Run System Commands)
Run Command
import subprocess
subprocess.run(["echo", "Hello World"])Capture Output
result = subprocess.run(["python", "--version"], capture_output=True, text=True)
print(result.stdout)8. JSON Utility (Data Handling)
Convert Data to JSON
import json
data = {"name": "John"}
print(json.dumps(data))9. TIME Module (Timing Operations)
Sleep Example
import time
print("Start")
time.sleep(2)
print("End")10. RANDOM Module (Random Values)
Generate Random Number
import random
print(random.randint(1, 10))11. Useful Utility Modules Overview
| Module | Purpose |
| os | System operations |
| sys | System parameters |
| pathlib | File paths |
| shutil | File operations |
| datetime | Date/time handling |
| logging | Logging system |
| subprocess | Run commands |
| json | Data handling |
| random | Random values |
| time | Time control |
Real-World Applications
Python utilities are used in:
- Automation scripts
- DevOps tools
- File management systems
- System monitoring tools
- Backend services
- Data processing pipelines
Best Practices
- Use
pathlibinstead of os.path - Use
logginginstead of print for apps - Handle exceptions properly
- Avoid hardcoded paths
- Use subprocess carefully for security
Common Mistakes
Using print instead of logging
Bad:
print("Error occurred")Better:
logging.error("Error occurred")Ignoring system permissions
File operations may fail without proper access rights.
Summary
Python provides a rich set of built-in tools and utilities that help developers manage files, system operations, logging, dates, and automation tasks efficiently.
Conclusion
Python utility modules are essential for real-world development. Mastering tools like os, sys, pathlib, and logging allows you to build powerful automation scripts and professional-grade applications.


0 Comments