Concepts / File and Archive Operations

File and Archive Operations

File and archive operations provide a useful setting for combining these three improvements.

  • Programming

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.

Key point

The three improvements are independent. You can add verbosity control, flexible command-line input, and built-in archive creation incrementally as a script grows.

In the real world

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.

sys.argvscript name, options, extrapathsarchive input listdefault pathsextra pathsmultiple argumentsarchive input listdefault paths plus extrapathsread argumentsextendadd multiple paths
How are command-line arguments stored in sys.argv, and how does list.extend add multiple extra files to the archive input list?
python

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.

command-lineoptions-vdetailed reportingfile detailsfiles being added-qquiet reportingerrorsnormal output suppresseddefault reportingneither flaginspectpresentotherwisepresentotherwise
How does the script's control flow change when it receives -v, -q, or neither flag, and what output is produced in each case?
python

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.

command-lineargumentsextra pathsarchive input listdefault and extra pathszipfile or tarfilebuilt-in archive modulearchive filecreated resultaddprovide selected pathscreate
How do selected files move from command-line arguments through the archive module into the resulting archive file?
ApproachExecution flowDesign effect
os.system callPython asks the operating system to run an external commandDepends on an external system command
zipfile or tarfilePython passes selected inputs to a built-in archive moduleRemoves 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.

command-lineoptionsflags and pathsselected inputsdefault plus extra pathsarchive modulezipfile or tarfilearchive resultcreated archivereporting modedetailed, quiet, or defaultinterpretsend inputsproducereport according to mode
What happens in order from parsing options and filenames to selecting files, creating the archive, and reporting the result?

Combining the Three Enhancements

Worked example

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.

  1. 1
    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.

  2. 2
    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.

  3. 3
    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.

  4. 4
    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.

Result

The script has three separable improvements: controlled output, flexible input, and archive creation through a built-in Python module.

Code walkthrough
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)
  1. 1
    Import sys so the script can inspect command-line arguments.
  2. 2
    Create the initial archive input list with a default directory.
  3. 3
    Check whether -v or -q was supplied.
  4. 4
    Collect command-line values that are not the two verbosity flags.
  5. 5
    Use extend to add those extra values to archive_inputs.
  6. 6
    Select detailed, quiet, or default reporting.
  7. 7
    The 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.
Output
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 like

    The internal archive list never changes, even when the user supplies additional command-line paths.

    Why it fails

    The script cannot accept flexible input from its user.

    Do this instead

    Retrieve 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 like

    The verbosity flags are copied into the list of paths to archive.

    Why it fails

    Options control reporting; they are not additional files or directories.

    Do this instead

    Separate 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 like

    The script continues to construct an os.system call for archive creation.

    Why it fails

    The script retains an external dependency and becomes less portable.

    Do this instead

    Use 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 like

    The -q option prevents every message from being reported.

    Why it fails

    The source behavior describes quiet mode as silent unless an error occurs.

    Do this instead

    Suppress normal output while preserving error reporting.

Best practice

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

MEDIUM

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.
  1. 1
    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.

Related concepts

More concepts starting with F