File and Archive Operations
Verbosity levels (-v and -q flags) let users control how much information a script outputs, making it suitable for different contexts.
From Working Script to Professional Tool
A script can solve its original problem and still be difficult to use. A more maintainable tool 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. File and archive operations provide a useful setting for combining these three improvements.
The three improvements are independent. You can add verbosity control, flexible command-line input, and built-in archive creation incrementally as a script grows.
Imagine a backup script with a default list of directories. A user can add -v to see detailed information about files being added, use -q to suppress normal output unless an error occurs, and provide additional directories as command-line arguments. The script can then create the archive with zipfile rather than calling an external archive command.
Building the Input List
Command-line arguments are retrieved from sys.argv. A script can inspect those arguments and add selected extra inputs to an internal list. The list.extend method is useful when several extra files or directories should be added together. This avoids hardcoding every possible input into the script.
In this generated example, archive_inputs begins with a default entry. The values taken from sys.argv are collected as extra_inputs, and extend adds those values to the existing list. The important design idea is that the script's internal list can grow from user input instead of containing only hardcoded names.
Controlling Output with Flags
Verbosity controls how much information a script reports. The -v option selects detailed output, such as information about each file being added. The -q option selects quiet behavior, where the script stays silent unless an error occurs. With neither option, the script can use its ordinary default reporting behavior.
The generated example separates the choice of reporting mode from the later archive work. Once report_mode has been selected, the file-processing part of the script can decide what to print without changing how the archive is assembled.
Creating Archives Inside Python
Archive creation can be handled by Python's built-in zipfile or tarfile modules instead of an os.system call. This removes reliance on an external system command, eliminates an external dependency, and improves portability. The selected files move from the script's input list into the chosen archive module, which creates the resulting archive.
| Approach | Execution flow | Design effect |
|---|---|---|
| os.system call | Python asks the operating system to run an external command | Depends on an external system command |
| zipfile or tarfile | Python passes selected inputs to a built-in archive module | Removes the external dependency and improves portability |
Combining the Three Enhancements
A Backup Script Design
Design the control flow for a backup script that has default directories, accepts -v or -q, accepts extra command-line paths, and creates an archive with a built-in module.
Choose the reporting mode: Inspect the command-line options. Select detailed reporting for -v, quiet reporting for -q, or the default reporting mode when neither option is supplied.
Build the input list: Start with the script's default directories. Retrieve additional command-line arguments from sys.argv and use extend to add the extra directories to the internal list.
Create the archive: Pass the selected inputs to a built-in archive module such as zipfile or tarfile instead of constructing an os.system archive command.
Report the result: Use the selected verbosity mode while reporting the archive work. Detailed mode can identify files being added, while quiet mode suppresses normal output unless an error occurs.
The script has three separable improvements: controlled output, flexible input, and archive creation through a built-in Python module.
import sys archive_inputs = ["default_directory"] verbose = "-v" in sys.argv quiet = "-q" in sys.argv extra_inputs = [item for item in sys.argv[1:] if item not in ("-v", "-q")] archive_inputs.extend(extra_inputs) if verbose: report_mode = "detailed" elif quiet: report_mode = "quiet" else: report_mode = "default" # The archive operation is intentionally represented as the next design step: # use zipfile or tarfile with archive_inputs, rather than os.system. print(report_mode) print(archive_inputs)
Mistakes Beginners Make
Hardcoding every directory or file that the script should process.
The script cannot accept flexible input from its user.
Fix:
Retrieve extra arguments from sys.argv and add them to the internal list with extend.Treating -v and -q as ordinary archive inputs.
Options control reporting; they are not additional files or directories.
Fix:
Separate option handling from the extra path values before extending the archive input list.Using an external archive command when a built-in module can perform the archive operation.
The script retains an external dependency and becomes less portable.
Fix:
Use a built-in module such as zipfile or tarfile for archive creation.Making quiet mode suppress errors as well as normal output.
The source behavior describes quiet mode as silent unless an error occurs.
Fix:
Suppress normal output while preserving error reporting.
Keep three responsibilities conceptually distinct: interpret options, construct the archive input list, and create or report the archive. This separation makes it easier to adopt each enhancement independently and to reason about what changes when a user supplies a flag or an extra path.
Practice and Review
Plan a backup script that starts with two default directories, accepts -v or -q, accepts any number of extra command-line paths, and creates an archive without os.system. Describe what happens to the reporting mode and input list when the user supplies -v followed by two extra directories.
Hints
- Separate the verbosity option from the extra path arguments.
- Use sys.argv as the source of command-line values.
- Use extend to add multiple extra paths to the default input list.
- Choose zipfile or tarfile as the built-in archive module.
- A maintainable file-and-archive script gives users control over output, input, and archive creation. The -v and -q options select detailed or quiet reporting. sys.argv provides command-line arguments, and extend adds multiple extra inputs to an internal list. zipfile and tarfile allow archive creation through Python's built-in modules rather than an os.system call. Because these improvements are independent, they can be introduced one at a time.
Key Takeaways
- Verbosity flags let users choose detailed, quiet, or default reporting.
- sys.argv provides command-line input, while list.extend adds multiple extra paths to an existing archive input list.
- Built-in modules such as zipfile and tarfile replace os.system archive commands and improve portability.
- Output control, flexible input, and built-in archive creation are independent improvements that can be adopted incrementally.