System Interaction Utilities
os Module (Operating System Interface)
The os module exposes cross-platform operating system functionality for file system operations, path management, and process control.
Common path and file manipulation functions:
os.path.dirname(target_path): Accepts a file path and returns its parent directory as a string, e.g. if input is/opt/app/services/processor.py, output is/opt/app/servicesos.path.abspath(target_path): Returns the absolute normalized path of the input file, resolving relative references and symlinksos.getcwd(): Takes no arguments, returns the current working directory of the running Python process__file__: Built-in attribute holding the path of the currently executing Python scriptsys.argv[0]: First element of the command line argument list, holding the path of the executed scriptos.path.exists(target_path): Returns a boolean indicating if the input path exists on the file systemos.path.join(*path_segments): Concatenates multiple path segments using OS-appropriate separators; if any segment after the first starts with a leading slash, all preceding segments are discardedos.curdir: Constant representing the current directory, equals.on all common operating systems
- What is the
__file__attribute? It stores the path used to load the current Python script. For example, if you createpath_test.pywith the following content:
import os
if __name__ == "__main__":
print(__file__)
Running python path_test.py from the same directory will output path_test.py.
-
Is the output of
print(__file__)consistent across all execution environments? No, it varies based on how the script is launched. When executed directly via the CLI, it returns the exact path passed to the Python interpreter. IDEs like PyCharm automatically resolve and inject the full absolute path for__file__during execution, even if you run it from the project root. -
How to reliably fetch the full absolute path of the current script? Use
os.path.abspath(__file__)to get a consistent absolute path regardless of execution environment. Sample output:/home/developer/projects/qa_suite/20240520/path_test.py -
How to get the parent directory of the current script? Two valid approaches:
- Method 1:
os.getcwd()returns the process working directory, which may differ from the script location if the script is launched from a different directory - Method 2 (Recommanded):
os.path.dirname(os.path.abspath(__file__))returns the actual parent directory of the script, regardless of the working directory at launch time
-
Note: After a Python script finishes execution, its parent directory is automatically added to the
sys.pathmodule search path for subsequent imports. -
Path concatenation example:
base_dir = "/opt/app/data"
sub_resource = "reports/2024/q2.csv"
full_path = os.path.join(base_dir, sub_resource)
print(full_path)
# Output: /opt/app/data/reports/2024/q2.csv
If a later segment has a leading slash, prior segments are ignored:
dir_a = "/opt/app/worker"
dir_b = "/tmp/cache/assets"
combined = os.path.join(dir_a, dir_b)
print(combined)
# Output: /tmp/cache/assets
- Check path existence:
import os
valid_path = "/home/developer/projects/qa_suite/20240520"
print(os.path.exists(valid_path))
# Output: True
invalid_path = "/home/developer/projects/qa_suite/20240520/nonexistent_dir"
print(os.path.exists(invalid_path))
# Output: False
- Create a missing directory:
You can either run a shell command via
os.system()or use the nativeos.makedirs()method:
new_dir = "/home/developer/projects/qa_suite/20240520/test_outputs"
os.makedirs(new_dir, exist_ok=True)
print(os.path.exists(new_dir))
# Output: True
-
Fetch the current executed script name:
sys.argv[0]and__file__return equivalent values for most execution scenarios. -
Fetch current working directory:
os.getcwd()andos.path.dirname(os.path.abspath(__file__))return equivalent values only if the script is launched from its own parent directory.
dotenv (Environment Variable Management)
The dotenv library loads configuration values from a .env file into system environment variables, separating sensitive credentials and environment-specific config from application code to improve security posture.
Store configuration in a .env file in your project root:
AWS_ACCESS_KEY_ID=AKIAU6GDZ7EXAMPLE
AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
ENVIRONMENT=staging
Load the environment variables at application startup:
from dotenv import load_dotenv, find_dotenv
import os
load_dotenv(find_dotenv())
# Access loaded variables
aws_access_key = os.getenv("AWS_ACCESS_KEY_ID")
aws_secret_key = os.getenv("AWS_SECRET_ACCESS_KEY")
deployment_env = os.getenv("ENVIRONMENT")
logging (Structured Logging)
The logging module provides flexible, configurable logging functionality that supports output to both the console and persistent log files, with configurable verbosity and formatting.
Log levels (ordered from lowest to highest verbosity):
DEBUG < INFO < WARNING < ERROR < CRITICAL
Only logs at or above the configured level will be emitted.
Core configuration via logging.basicConfig():
level: Minimum log level to emit, e.g.logging.INFOfilename: Path to the output log file (if omitted, logs are printed to stdout)filemode: Write mode for the log file,afor append (preserve existing logs) orwfor overwrite (clear existing logs on startup)format: String defining the structure of each log entry, with supported placeholders:%(levelname)s: Human-readable log level name%(asctime)s: Timestamp of log emission%(filename)s: Name of the script generating the log%(lineno)d: Line number where the log was called%(funcName)s: Name of the function generating the log%(process)d: ID of the process generating the log%(thread)d: ID of the thread generating the log%(message)s: User-provided log content
Console logging demo:
import logging
logging.basicConfig(
level=logging.INFO,
filemode="a",
format="%(levelname)s | %(asctime)s | %(filename)s:%(lineno)d | %(message)s"
)
if __name__ == "__main__":
logging.debug("Debug-level trace message")
logging.info("Normal operation event logged")
logging.warning("Low-priority non-breaking issue detected")
logging.error("Operation failed with recoverable error")
logging.critical("Fatal error causing service shutdown")
Sample output:
INFO | 2024-05-20 14:32:01,245 | log_test.py:11 | Normal operation event logged
WARNING | 2024-05-20 14:32:01,245 | log_test.py:12 | Low-priority non-breaking issue detected
ERROR | 2024-05-20 14:32:01,246 | log_test.py:13 | Operation failed with recoverable error
CRITICAL | 2024-05-20 14:32:01,246 | log_test.py:14 | Fatal error causing service shutdown
File logging demo:
import logging
import os
LOG_STORAGE_DIR = "/home/developer/projects/qa_suite/logs"
os.makedirs(LOG_STORAGE_DIR, exist_ok=True)
log_file_path = os.path.join(LOG_STORAGE_DIR, "service_runtime.log")
logging.basicConfig(
level=logging.INFO,
filename=log_file_path,
filemode="a",
format="%(levelname)s | %(asctime)s | %(filename)s:%(lineno)d | %(message)s"
)
if __name__ == "__main__":
logging.debug("Debug-level trace message")
logging.info("Normal operation event logged")
logging.warning("Low-priority non-breaking issue detected")
logging.error("Operation failed with recoverable error")
logging.critical("Fatal error causing service shutdown")
Sample log file content:
INFO | 2024-05-20 14:35:22,671 | log_test.py:16 | Normal operation event logged
WARNING | 2024-05-20 14:35:22,672 | log_test.py:17 | Low-priority non-breaking issue detected
ERROR | 2024-05-20 14:35:22,672 | log_test.py:18 | Operation failed with recoverable error
CRITICAL | 2024-05-20 14:35:22,673 | log_test.py:19 | Fatal error causing service shutdown
MySQLdb (MySQL Database Connector)
The MySQLdb library provides native interfaces for interacting with MySQL databases, supporting DDL operations, data reads, writes, and transaction management.
Basic database operation workflow:
- Establish a connection to the target database
- Create a cursor object to execute queries
- Run SQL statements and fetch results
- Close the connection to release resources
Sample repeated query execution:
import MySQLdb
MYSQL_HOST = "192.168.1.120"
MYSQL_PORT = 55219
MYSQL_USER = "db_admin"
MYSQL_PASSWD = "SecurePass123!@"
def run_repeated_sql(query: str, execution_count: int):
# Initialize database connection
db_conn = MySQLdb.connect(
host=MYSQL_HOST,
port=MYSQL_PORT,
user=MYSQL_USER,
passwd=MYSQL_PASSWD,
charset="utf8"
)
cursor = db_conn.cursor()
for _ in range(execution_count):
cursor.execute(query)
result = cursor.fetchone()
print(f"Query returned: {result}")
# Clean up connection
db_conn.close()
if __name__ == "__main__":
test_query = "SELECT VERSION();"
run_count = 10
run_repeated_sql(test_query, run_count)
Math and Utility Libraries
random (Pseudo-Random Number Generator)
The random module provides functions for generating pseudo-random values for testing, sampling, and simulation use cases.
- Generate a random integer between 0 and 100 inclusive:
import random
print(random.randint(0, 100))
# Sample output: 42
- Generate a random integer between 0 and 100 inclusive that is divisible by 5:
print(random.randrange(0, 101, 5))
# Sample outputs: 15, 70, 0, 95
- Pick a random character from a defined set:
allowed_chars = "abcdef123!@#"
print(random.choice(allowed_chars))
# Sample outputs: 'f', '3', '@', 'b'