Scripting Skills

Features

Feature
Description
Use Case

CLI with argparse

Command-line argument parsing for building robust CLI tools.

Building user-facing scripts (e.g., python greet.py John)

os module

Interacting with OS: path manipulation, directories, file operations.

Automating file/folder operations like creating backups or organizing directories.

subprocess module

Executes shell commands inside Python.

Automating command-line tasks, shell scripting, running bash or system utilities.

Environment Variables

Accessing or modifying system environment variables.

Loading secrets/configs dynamically, adapting script behavior based on environment.

shutil for file ops

High-level file operations like copy, move, archive.

Backup, restore, zip/unzip automation, temporary file cleanup.

Logging (logging)

Replaces print with log levels (info, debug, error).

Debugging and monitoring script behavior in production or automation pipelines.

Shebang Line

#!/usr/bin/env python3 lets a script be run as an executable on Unix-like systems.

Run scripts directly like ./myscript.py from terminal.

Exit Codes (sys.exit)

Return specific exit codes for success or failure in automation pipelines.

Integrating with CI/CD or cronjobs where exit codes signal job status.

pathlib

Modern, OOP way of handling file paths (better than os.path).

Cleaner syntax for cross-platform file manipulation.

Cross-platform Compatibility

Consider differences in OS, paths, and encoding.

Make scripts usable on both Windows and Linux/macOS.


Code sample

import argparse

parser = argparse.ArgumentParser(description="Greet someone.")
parser.add_argument("name", help="Name of the person")
args = parser.parse_args()

print(f"Hello, {args.name}!")
import logging

logging.basicConfig(level=logging.INFO)
logging.info("This is an info message.")
logging.error("This is an error message.")

Last updated