Automation & scripting
CLI tools, scheduled jobs, filesystem tasks, and glue scripts that save time.
How to Build a CLI with argparse in Python
Create a beginner-friendly command-line tool in Python that processes multiple filenames with optional flags for verbose output and uppercase conversion.
import argparse
def main():
parser = argparse.ArgumentParser(
description="A simple CLI to process files with optional verbose mode."
)
parser.add_argument("filenames", nargs="+", help="Files to process")
parser.add_argument("-v", "--verbose", action="store_true", help="Print extra details")
…
How to Build a Python argparse CLI for Beginners
Build a beginner-friendly command-line interface using Python's argparse module with positional and optional arguments.
import argparse
def greet(name, greeting="Hello", uppercase=False):
message = f"{greeting}, {name}!"
if uppercase:
message = message.upper()
return message
def main():
parser = argparse.ArgumentParser(description="A simple CLI greet tool for beginners.")
parser.add_argument("name", help="…
How to Build a Simple Python CLI with argparse
Create a friendly command-line greeting tool with argparse that accepts a positional name and optional flags for custom greetings and uppercase output.
import argparse
def greet(name, greeting="Hello", uppercase=False):
message = f"{greeting}, {name}!"
return message.upper() if uppercase else message
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="A simple greeting tool to demonstrate argparse basics."
)
parser.…
How to Build a Simple argparse CLI in Python
Create a beginner-friendly command-line tool with argparse that reads a file, optionally uppercases its lines, and prints a configurable number of lines.
import argparse
def main():
parser = argparse.ArgumentParser(
description="Automate file processing with a simple CLI tool."
)
parser.add_argument("filename", help="Path to the input file")
parser.add_argument("--uppercase", action="store_true", help="Convert text to uppercase")
parser.add…
How to Build a Simple argparse CLI in Python
Build a beginner-friendly command-line tool with argparse that greets a user, with optional greeting text and uppercase output.
import argparse
def greet(name, greeting="Hello", uppercase=False):
message = f"{greeting}, {name}!"
if uppercase:
message = message.upper()
return message
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Simple CLI greeting tool")
parser.add_argument("name", help=…
How to Build an argparse CLI That Filters File Lines by Keyword in Python
This Python script is a command-line tool built with argparse that reads a text file and prints only the lines that contain (or don't contain) a given keyword.
import argparse
import sys
def main():
parser = argparse.ArgumentParser(description="Filter lines from a file by keyword.")
parser.add_argument("input", type=str, help="File to read")
parser.add_argument("keyword", type=str, help="Keyword to filter lines")
parser.add_argument("--contains", action="sto…
How to Build an argparse Command-Line Tool in Python
Create a simple file-info CLI with argparse that counts lines and prints file size, with optional verbose and output flags.
import argparse
import os
from pathlib import Path
def process_file(filepath, verbose=False):
"""Read a file and report its size and line count."""
path = Path(filepath)
if not path.exists():
raise FileNotFoundError(f"File not found: {filepath}")
content = path.read_text()
lines = conten…
How to Create a Simple Python CLI with argparse
Build a beginner-friendly command-line tool with argparse that accepts positional and optional arguments to greet users flexibly.
import argparse
def greet(name, greeting="Hello", uppercase=False):
message = f"{greeting}, {name}!"
return message.upper() if uppercase else message
def main():
parser = argparse.ArgumentParser(
description="A simple CLI tool that greets users."
)
parser.add_argument(
"name",
…
How to Implement argparse CLI Command in Python
Build a beginner-friendly command-line tool with argparse that accepts positional and optional arguments, flags, and prints a customizable greeting.
import argparse
def main():
parser = argparse.ArgumentParser(description="A simple CLI tool to greet users.")
parser.add_argument("name", help="Your name")
parser.add_argument("-g", "--greeting", default="Hello", help="Greeting word (default: Hello)")
parser.add_argument("--uppercase", action="store_…
How to Parse CLI Arguments in Python with argparse
Build a beginner-friendly CLI with argparse that accepts optional --name, --greeting, and --uppercase flags, then prints a customizable greeting.
import argparse
def main():
parser = argparse.ArgumentParser(description="Greet a user with optional customization.")
parser.add_argument("--name", default="world", help="Name to greet")
parser.add_argument("--greeting", default="Hello", help="Greeting word")
parser.add_argument("--uppercase", action=…
How to Sort Command-Line Arguments in Python
Build a beginner-friendly argparse CLI that sorts numbers or words passed as arguments, with an optional reverse flag.
import argparse
def main():
parser = argparse.ArgumentParser(description="Sort numbers or words from the command line.")
parser.add_argument("items", nargs="+", help="Items to sort (numbers or words)")
parser.add_argument("--reverse", "-r", action="store_true", help="Sort in descending order")
args =…
How to validate argparse CLI commands in Python
Build a beginner-friendly command-line argument parser with argparse, including required and optional arguments, plus simple validation for age.
import argparse
def main():
parser = argparse.ArgumentParser(description="Validate CLI arguments for beginners.")
parser.add_argument("name", type=str, help="Your name.")
parser.add_argument("--age", type=int, default=None, help="Your age (optional).")
parser.add_argument("--verbose", action="store_t…
Browse by section
Each section groups closely related Python snippets.
Automation & scripting — Python code examples
What you will find here
This page collects automation & scripting snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.