Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Building CLI applications


Our focus from the initial script example in Chapter 1, Getting Started, was on using CLI scripting to learn Python basics. CLI applications have a number of common features:

  • They often read from the standard input file, write to a standard output file, and produce logs or errors in the standard error file. The OS assures us that these files are always available. Python provides them as sys.stdin, sys.stdout, and sys.stderr. Furthermore, functions such as input() and print() use these files by default.

  • They often use environment variables for configuration. These values are available through os.environ.

  • They may also rely on shell features, like expanding ~ into a user's home directory, something done by os.path.expanduser().

  • They often parse command-line arguments. While the variable sys.argv has the argument strings, these are awkward to work with directly. We'll use the argparse module to define the argument patterns, parse the strings, and create an object with...