Python tutorials > Modules and Packages > Standard Library > What are key Standard Library modules?
What are key Standard Library modules?
Introduction to Key Standard Library Modules
os
: Operating System Interaction
os
module provides functions for interacting with the operating system. Here's a breakdown of the example code:
os.getcwd()
: Returns the current working directory.os.mkdir(path)
: Creates a new directory at the specified path.os.listdir(path)
: Returns a list of all files and directories in the specified path.os.rmdir(path)
: Removes an empty directory at the specified path. Caution: This function deletes the directory.
import os
# Get the current working directory
current_directory = os.getcwd()
print(f"Current Directory: {current_directory}")
# Create a new directory
new_directory = "new_directory"
if not os.path.exists(new_directory):
os.mkdir(new_directory)
print(f"Directory '{new_directory}' created.")
# List files in a directory
files = os.listdir(current_directory)
print(f"Files in current directory: {files}")
# Remove a directory (be careful!)
# os.rmdir(new_directory) # Uncomment to remove the directory
# print(f"Directory '{new_directory}' removed.")
datetime
: Date and Time Handling
datetime
module allows you to work with dates and times. Here's a breakdown:
datetime.datetime.now()
: Returns the current date and time.strftime(format)
: Formats a datetime object into a string according to the specified format code. Common format codes include: %Y
(year), %m
(month), %d
(day), %H
(hour), %M
(minute), %S
(second).datetime.datetime.strptime(date_string, format)
: Parses a string into a datetime object according to the specified format code.
import datetime
# Get the current date and time
now = datetime.datetime.now()
print(f"Current Date and Time: {now}")
# Format the date and time
formatted_date = now.strftime("%Y-%m-%d %H:%M:%S")
print(f"Formatted Date: {formatted_date}")
# Create a datetime object
date_string = "2023-12-25"
datetime_object = datetime.datetime.strptime(date_string, "%Y-%m-%d")
print(f"Datetime Object: {datetime_object}")
math
: Mathematical Operations
math
module provides mathematical functions and constants. Key aspects are:
math.sqrt(x)
: Returns the square root of x.math.pow(x, y)
: Returns x raised to the power of y.math.pi
: Represents the mathematical constant pi (π).
import math
# Calculate the square root
sqrt_value = math.sqrt(16)
print(f"Square Root of 16: {sqrt_value}")
# Calculate the power
power_value = math.pow(2, 3)
print(f"2 raised to the power of 3: {power_value}")
# Use mathematical constants
pi_value = math.pi
print(f"Value of Pi: {pi_value}")
json
: Data Serialization
json
module is used for serializing and deserializing JSON (JavaScript Object Notation) data.
json.dumps(obj)
: Converts a Python object to a JSON string.json.loads(json_string)
: Converts a JSON string to a Python object (usually a dictionary or a list).
import json
# Python dictionary
data = {
"name": "Alice",
"age": 30,
"city": "New York"
}
# Convert Python dictionary to JSON string
json_string = json.dumps(data)
print(f"JSON String: {json_string}")
# Convert JSON string to Python dictionary
python_dict = json.loads(json_string)
print(f"Python Dictionary: {python_dict}")
re
: Regular Expressions
re
module enables you to work with regular expressions for pattern matching.
re.search(pattern, string)
: Searches for the first occurrence of a pattern in a string. Returns a match object if found, otherwise None.re.findall(pattern, string)
: Finds all occurrences of a pattern in a string and returns them as a list.
import re
# Search for a pattern in a string
text = "The quick brown fox jumps over the lazy dog"
pattern = "fox"
match = re.search(pattern, text)
if match:
print(f"Pattern '{pattern}' found at index: {match.start()}")
else:
print(f"Pattern '{pattern}' not found.")
# Find all occurrences of a pattern
pattern = "o"
all_matches = re.findall(pattern, text)
print(f"All occurrences of '{pattern}': {all_matches}")
urllib
: Networking
urllib
module is for fetching data from URLs.
urllib.request.urlopen(url)
: Opens a connection to the specified URL.response.read()
: Reads the content from the response.urllib.error.URLError
if the URL is invalid or the connection fails. Handling these exceptions is crucial for robust network applications.
import urllib.request
# Fetch data from a URL
url = "https://www.example.com"
try:
with urllib.request.urlopen(url) as response:
html = response.read()
# Print the first 200 characters of the HTML content
print(html[:200].decode('utf-8'))
except urllib.error.URLError as e:
print(f"Error fetching URL: {e}")
Concepts Behind the Snippets
os.getcwd()
instead of low-level system calls).
Real-Life Use Case Section
os
module can manage file uploads and storage.datetime
module can schedule tasks and manage user sessions.json
module handles API requests and responses.re
module validates user input (e.g., email addresses).urllib
module retrieves data from external APIs.
Best Practices
from module import function
.FileNotFoundError
, URLError
).
Interview Tip
os
, datetime
, json
, and re
, and be able to explain how you have used them in your projects.
When to use them
Memory Footprint
Alternatives
os
: pathlib
provides an object-oriented approach to file system paths.datetime
: arrow
and pendulum
are easier-to-use date and time manipulation libraries.urllib
: requests
is a more user-friendly HTTP library.
Pros
Cons