π Mastering Python Modules & File Handling: From Imports to Built-in Power Tools

As you level up in Python, one thing becomes clear β you donβt have to write everything from scratch! Python comes packed with modules that offer reusable code, tools, and utilities. Combine that with powerful file handling features, and you're ready to build real-world apps!
In this article, weβll break down:
π§© Understanding Python Modules: Creation, Import, and Renaming
π Exploring Essential Built-in Python Modules:
datetime
,math
,os
, and moreπ Introduction to File Handling in Python: Opening and Managing Files
π§© Understanding Python Modules: Creation, Import, and Renaming
π¦ What is a Python Module?
A module is simply a Python file (.py
) that contains functions, classes, or variables which you can reuse in other Python scripts.
πΉ 1. Creating a Module
Letβs create a custom module named utils.py
# utils.py
def greet(name):
return f"Hello, {name}!"
πΉ 2. Importing a Module
Now, use import
to bring in your custom module.
import utils
print(utils.greet("Jugal"))
πΉ 3. Import Specific Functions
from utils import greet
print(greet("Kishore"))
πΉ 4. Renaming Modules
import utils as u
print(u.greet("Coder"))
β Why use renaming?
To shorten names (
numpy
asnp
)To avoid naming conflicts
π Exploring Essential Built-in Python Modules
Python offers hundreds of built-in modules. Letβs explore the most essential ones for beginners:
π datetime
β Work with Dates and Times
import datetime
now = datetime.datetime.now()
print("Current Date & Time:", now)
today = datetime.date.today()
print("Todayβs Date:", today)
β math
β Advanced Math Functions
import math
print(math.sqrt(16)) # Square root
print(math.factorial(5)) # 5!
print(math.pi) # Ο
π os
β Interact with the Operating System
import os
print("Current Directory:", os.getcwd())
print("Files in dir:", os.listdir())
# Create folder
os.mkdir("new_folder")
π random
β Generate Random Numbers
import random
print(random.randint(1, 10)) # Random int between 1 and 10
print(random.choice(["red", "blue", "green"])) # Random choice
π statistics
β Basic Data Stats
import statistics
data = [1, 2, 3, 4, 5]
print("Mean:", statistics.mean(data))
print("Median:", statistics.median(data))
π Introduction to File Handling in Python: Opening and Managing Files
Python makes it super easy to read from and write to files β great for building apps that handle data.
πΉ Opening a File
f = open("example.txt", "r")
print(f.read())
f.close()
πΉ Writing to a File
f = open("example.txt", "w")
f.write("Hello, world!")
f.close()
πΉ Reading Line-by-Line
f = open("example.txt", "r")
for line in f:
print(line.strip())
f.close()
πΉ Using with
Statement (Best Practice)
Automatically handles closing the file.
with open("example.txt", "r") as f:
content = f.read()
print(content)
πΉ Append Mode
with open("example.txt", "a") as f:
f.write("\nAppended line.")
π Summary Table
Feature | Example Used |
Module Creation | utils.py with greet() function |
Built-in Module | datetime , math , os , random |
File Handling | Reading, writing, appending files |
π Conclusion
Python's modular system and file handling capabilities make it a powerhouse for developers. By using custom and built-in modules, you reduce redundancy. With file I/O, your scripts can persist and manage data like a pro.
β
Practice creating your own modules
β
Explore built-in Python power tools
β
Use file handling to build data-driven apps
Subscribe to my newsletter
Read articles from Jugal kishore directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
