File and Archive Operations
File and archive operations provide a useful setting for combining these three improvements.
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 |
The important difference is whether archive creation is delegated to an external system command or handled by a Python built-in module.
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.
- 1Choose 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.
- 2Build the input list
Start with the script's default directories. Retrieve additional command-line arguments from
sys.argvand use extend to add the extra directories to the internal list. - 3Create the archive
Pass the selected inputs to a built-in archive module such as zipfile or tarfile instead of constructing an
os.systemarchive command. - 4Report 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)- 1Import sys so the script can inspect command-line arguments.
- 2Create the initial archive input list with a default directory.
- 3Check whether -v or -q was supplied.
- 4Collect command-line values that are not the two verbosity flags.
- 5Use extend to add those extra values to
archive_inputs. - 6Select detailed, quiet, or default reporting.
- 7The archive creation step should use zipfile or tarfile according to the chosen archive format.
- The default list provides the starting inputs.
- The flags select the reporting mode.
- The remaining command-line values become extra inputs.
- extend adds all extra inputs to the existing list.
The generated example displays the selected reporting mode and the completed archive input list.Mistakes Beginners Make
Common mistake Hardcoding every directory or file that the script should process.
What it looks likeThe internal archive list never changes, even when the user supplies additional command-line paths.
Why it failsThe script cannot accept flexible input from its user.
Do this insteadRetrieve extra arguments from sys.argv and add them to the internal list with extend.
Common mistake Treating -v and -q as ordinary archive inputs.
What it looks likeThe verbosity flags are copied into the list of paths to archive.
Why it failsOptions control reporting; they are not additional files or directories.
Do this insteadSeparate option handling from the extra path values before extending the archive input list.
Common mistake Using an external archive command when a built-in module can perform the archive operation.
What it looks likeThe script continues to construct an os.system call for archive creation.
Why it failsThe script retains an external dependency and becomes less portable.
Do this insteadUse a built-in module such as zipfile or tarfile for archive creation.
Common mistake Making quiet mode suppress errors as well as normal output.
What it looks likeThe -q option prevents every message from being reported.
Why it failsThe source behavior describes quiet mode as silent unless an error occurs.
Do this insteadSuppress 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.argvas 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.
- 1A 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.argvprovides 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 anos.systemcall. 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.argvprovides 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.systemarchive commands and improve portability. - Output control, flexible input, and built-in archive creation are independent improvements that can be adopted incrementally.