Linux | Python Template

Template for a Python script that includes logging and argparse. You can use this as a starting point for your projects:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/python
# -*- coding: utf-8 -*-

import argparse
import logging

def setup_logging(level=logging.INFO):
"""Set up logging configuration."""
logging.basicConfig(
level=level,
format='%(asctime)s - %(levelname)s - %(message)s'
)

def main(args):
"""Main function to execute the script."""
logging.info("Starting the script...")

# Example of using the command-line arguments
if args.verbose:
logging.setLevel(logging.DEBUG)
logging.debug("Verbose mode is on.")

logging.info(f"Argument value: {args.value}")

# Add your script logic here
# ...

logging.info("Script finished successfully.")

if __name__ == "__main__":
# Set up argument parsing
parser = argparse.ArgumentParser(description="A template for a Python script.")
parser.add_argument(
'-v', '--value',
type=str,
required=True,
help='An example argument value.'
)
parser.add_argument(
'-vv', '--verbose',
action='store_true',
help='Enable verbose logging.'
)

args = parser.parse_args()

# Set up logging
setup_logging()

# Run the main function
main(args)

Linux | Python Template
https://waipangsze.github.io/2025/07/23/Linux-Python-Template/
Author
wpsze
Posted on
July 23, 2025
Updated on
July 23, 2025
Licensed under