Concepts / Error Handling in Scripts

Error Handling in Scripts

Verbosity levels (-v and -q flags) let users control how much information a script outputs, making it suitable for different contexts.

  • Programming

From Working Script to Usable Tool

A script can solve its original problem and still be difficult to use. A more professional script gives users control over how much information they see, accepts additional input from the command line, and uses Python's built-in tools instead of depending on external system commands. These improvements are useful for a backup script: users may want detailed progress while working interactively, almost no output during automation, extra directories included in one run, and archive creation that does not rely on os.system.

The three improvements are independent. You can add verbosity control, flexible command-line input, and built-in archive creation incrementally rather than rewriting the entire script at once.

Output States with -v and -q

Verbosity controls how much information a script outputs. The -v option selects a more detailed mode, while the -q option selects a quieter mode. In the backup-script example, verbose mode reports detailed information about each file being added. Quiet mode stays silent unless an error occurs. This gives one script two useful behaviors: visible progress for an interactive user and minimal output for an automated context.

selectselectDefault modenormal output-vdetailed file information-qerrors only
How do the -v and -q options change which messages a script outputs?

Choosing the Output Mode

A backup script is run once with -v and once with -q. What should the user expect to see?

Run with -v: The script uses verbose behavior and prints detailed information about each file being added.

Run with -q: The script uses quiet behavior and does not print ordinary progress information; it reports an error if one occurs.

Compare the purposes: Verbose output helps an interactive user inspect progress, while quiet output is suitable when routine messages should not appear.

The same backup operation can serve different contexts by changing the selected verbosity level.

Reading sys.argv Positions

Command-line arguments are retrieved from sys.argv. Treat sys.argv as the script's incoming command-line list: the script examines the values supplied by the user, identifies an option such as -v or -q, and identifies any additional arguments that should become input. This avoids hardcoding every directory or file name into the script.

next valuenext valuenext valuePosition 0script namePosition 1-v or -qPosition 2extra directoryPosition 3another extra directory
What does each position in sys.argv contain when a script is run with a flag and extra arguments?

The list.extend method adds the values from another collection to an existing list. In this design, a script begins with a default list of directories and then extends that list with extra command-line arguments. The important distinction is that the script keeps its defaults while accepting additional input; the user does not need to edit the source code to add another directory for one run.

Replacing External Archive Commands

A script that calls os.system depends on an external system command to create its archive. The source recommends replacing that approach with Python's built-in zipfile or tarfile modules. Using these modules eliminates the external dependency and improves portability, because archive creation is handled by Python's own tools rather than by a command that must be available in the surrounding operating system.

pass tocreatepass tocreateBackup inputsdirectories and filesBackup inputsdirectories and filesos.systemexternal commandzipfile or tarfilePython built-in moduleArchivedepends on system commandArchivehandled by Python
How does archive creation flow differently when a script calls os.system compared with using Python's zipfile or tarfile modules?

Prefer the built-in archive module when the script's job is archive creation. This keeps the archive operation inside the Python program and makes the script less dependent on the environment where it runs.

Combining the Three Enhancements

A Flexible Backup Run

Design the behavior of a backup script that has default directories, accepts a verbosity flag, accepts extra directories, and creates an archive with a built-in Python module.

Choose output behavior: The script checks whether the user selected -v or -q. The first choice enables detailed information about files being added; the second keeps ordinary output silent while still allowing errors to be reported.

Read command-line input: The script retrieves command-line arguments from sys.argv and separates the verbosity choice from additional input values.

Extend the backup list: The script starts with its default directories and uses extend to add the extra directories supplied by the user.

Create the archive: The script sends the resulting backup inputs through zipfile or tarfile instead of calling os.system.

Observe the design benefit: The script is now adjustable at run time, accepts flexible input, and avoids an external archive command. Each improvement could also have been introduced separately.

The backup script becomes a more maintainable tool without changing the fact that its central task is creating a backup archive.

Trace the data in order: the command line supplies the mode and extra inputs; sys.argv makes those values available; extend adds extra directories to the default list; the selected archive module receives the backup inputs; and the verbosity choice controls how much progress information the user sees. Keeping these responsibilities distinct makes the enhancements easier to adopt incrementally.

Mistakes to Avoid

  • Treating -v and -q as unrelated scripts

    The purpose of verbosity levels is to let one script adapt its output to different contexts.

    Fix: Use the selected flag to control the script's output behavior.

  • Hardcoding every directory that can be backed up

    The script loses the flexibility provided by command-line input.

    Fix: Retrieve extra arguments from sys.argv and add them to the internal list with extend.

  • Assuming quiet mode means that errors should disappear

    The quiet behavior described for the backup script remains silent unless an error occurs.

    Fix: Suppress routine output while preserving error reporting.

  • Replacing os.system without replacing the archive flow

    The script still needs an archive operation after its inputs have been collected.

    Fix: Use the zipfile or tarfile built-in module for archive creation.

Practice the State Trace

MEDIUM

Consider a backup script with a default list of directories. Describe the state of the script after a user selects -v, supplies two extra directories, and chooses the zipfile module for archive creation. Then describe what changes when the same run uses -q instead.

Hints
  • Identify which part of the design controls output.
  • Identify where the extra directories come from.
  • Explain what extend changes in the internal list.
  • Explain why using zipfile changes the dependency on an external system command.

A useful answer should distinguish three states: the selected output mode, the expanded list of backup inputs, and the archive mechanism used to process those inputs.

Practical Takeaways

  1. Use -v and -q to let users choose between detailed output and quiet operation.
  2. Retrieve command-line arguments from sys.argv instead of hardcoding every input.
  3. Use list.extend to add extra command-line values to an existing internal list.
  4. Prefer zipfile or tarfile over os.system for archive creation when a built-in Python module fits the task.
  5. Adopt these improvements independently and incrementally as the script grows.

Key Takeaways

  • Verbosity flags make one script suitable for both detailed interactive use and quiet automated use.
  • sys.argv supplies command-line input, while list.extend adds extra values to the script's existing list.
  • zipfile and tarfile provide built-in alternatives to os.system for archive creation.
  • The three enhancements are independent and can be introduced one at a time.