Debugging File and Directory Operations
Hierarchical file-naming organizes backups into a main directory containing date-based subdirectories, each containing time-stamped zip files, making backups easier to manage and verify.
From Clutter to Structure
A backup script can work correctly and still be difficult to maintain. If every backup is saved in one flat directory with a long timestamp-based filename, repeated use can produce dozens of similarly named files. Finding a backup from a particular day or checking whether a daily backup exists becomes harder. The maintenance improvement is to organize backups by date first and by time second.
The structure has three levels: a main backup directory, a date-based subdirectory, and a ZIP file whose name represents the backup time. For example, the date directory 20240115 represents January 15, 2024, while 143022.zip represents 2:30:22 PM. Multiple backups from the same day can therefore share one date directory while keeping separate time-stamped archive files.
Tracing the Directory Decision
Before an archive can be saved, the script must know whether the current date directory is already available. This creates a two-branch decision. If the directory exists, the script can use it. If it does not exist, the script creates it before continuing.
if not os.path.exists(date_directory): os.mkdir(date_directory)
Turning Time into Names
The directory and file names are produced from formatted representations of the current date and time. Python's time.strftime function takes a format code and returns a string representation of the current date or time. In this backup design, the date format is %Y%m%d and the time format is %H%M%S.
| Format code | Purpose | Role in the backup structure |
|---|---|---|
| %Y%m%d | Formatted date | Becomes the date subdirectory name |
| %H%M%S | Formatted time | Becomes the ZIP filename before the .zip extension |
The two time.strftime formats used by the hierarchical backup approach.
One Date, Two Backup Times
A backup runs on January 15, 2024 at 2:30:22 PM and then again at 6:05:00 PM. How does the hierarchical naming change?
Format the date: The date format %Y%m%d produces 20240115. Both backups occur on the same date, so both use the 20240115 subdirectory.
Format the first time: The first time produces 143022, so the first archive can be named 143022.zip.
Format the second time: The later time produces 180500, so the second archive can be named 180500.zip.
Compare the paths: The date portion remains the same while the time-based filename changes.
The same date directory contains separate time-stamped files: 20240115/143022.zip and 20240115/180500.zip.
From Paths to Archives
After the date directory is available and the time-based filename has been determined, the script constructs a system command. The command includes the full target path for the ZIP file and the source locations to be archived. The sources are joined into one space-separated string, and the resulting command is passed to os.system.
For example, if the target is /Users/swa/backup/20240115/143022.zip and the source is /Users/swa/notes, the constructed command is zip -r /Users/swa/backup/20240115/143022.zip /Users/swa/notes. os.system executes that command as an operating-system command.
The return value from os.system provides a basic completion check: 0 indicates success, while a non-zero value indicates failure. The script can use this result to verify whether the backup operation completed.
Debugging the Expected Path
Debugging this process means tracing each expected state in order. First identify the formatted date. Next confirm that the main backup directory and date directory form the intended location. Then identify the formatted time and append the .zip extension. Finally inspect the constructed command and the return value from os.system. This path-based trace makes it easier to determine whether a problem occurred during naming, directory preparation, command construction, or archive creation.
Verifying a Daily Backup
Trace a backup intended for January 15, 2024 at 2:30:22 PM.
Check the date value: The date string should be 20240115, which identifies the date-based subdirectory.
Check the directory decision: Use os.path.exists to determine whether the 20240115 directory is already present. If it is missing, use os.mkdir to create it.
Check the archive name: The time string should be 143022, producing the filename 143022.zip.
Check the complete target: The target should combine the main backup directory, the 20240115 date directory, and the 143022.zip file.
Check command completion: After os.system executes the ZIP command, a return value of 0 indicates success; a non-zero value indicates failure.
A successful trace ends with a time-stamped ZIP archive in the expected date-based directory and an os.system result of 0.
Mistakes to Catch
Creating the date directory every time
A repeated backup on the same day encounters a directory that has already been created.
Fix:
Use os.path.exists first, and call os.mkdir only when the directory is missing.Putting the full timestamp into one flat filename
Repeated daily backups create a cluttered flat list that is harder to search and verify.
Fix:
Use the date as the subdirectory name and the time as the ZIP filename.Using the time value for the directory and the date value for the file
The intended hierarchy is date-based directories containing time-stamped files.
Fix:
Use %Y%m%d for the date directory and %H%M%S for the ZIP filename.Ignoring the os.system return value
Command construction and command success are separate stages.
Fix:
Check whether os.system returned 0 or a non-zero value.
Practice the Trace
Suppose the current date is January 15, 2024 and the backup runs at 6:05:00 PM. Describe the date directory, the ZIP filename, and the directory decision for a first backup of that day versus a later backup on the same day.
Hints
- Apply %Y%m%d to the date.
- Apply %H%M%S to the time.
- For the first backup, decide what os.path.exists reports before os.mkdir runs.
- For the later backup, decide whether os.mkdir should run again.
What do you think happens?
What happens when the backup runs again on January 15, 2024 at 6:05:00 PM after an earlier backup already created the date directory?
Reveal answer
Answer: The existing date directory is reused and a different time-stamped ZIP file is selected.
The date remains 20240115, while the later time produces 180500. The existence check prevents another creation attempt for the already existing date directory.
Key Takeaways
- A hierarchical backup uses a main directory, date-based subdirectories, and time-stamped ZIP files.
- os.path.exists checks whether the date directory is present, while os.mkdir creates it only when necessary.
- time.strftime('%Y%m%d') produces the date directory name, and time.strftime('%H%M%S') produces the time-based archive name.
- The ZIP command combines the target path with the source paths and is executed through os.system.
- An os.system return value of 0 indicates success; a non-zero value indicates failure.
Key Takeaways
- Organize backups hierarchically instead of keeping every archive in one flat directory.
- Check for a date directory before creating it so repeated backups can use the same directory safely.
- Use formatted date and time strings to create predictable names.
- Construct the ZIP command from the target and source paths, then use the os.system result to verify completion.