liveBook · Manning

File archiving

As explained in The Quick Python Book, Fourth Edition

File archiving is the practice of collecting and preserving files in a managed archive for storage or later retrieval.

File archiving keeps incoming files available after processing. For a daily feed, moving each set out of the working directory lets the next set arrive without discarding data needed to check processing accuracy or track changes over time. section 20.2

Choose an archive layout according to how you need to find and store the files:

  • Add the date to each filename and move the files into one archive directory. This is simple, but related files become harder to find as sets accumulate. section 20.2 section 20.3
  • Create a date-named subdirectory for each set. Keep the original filenames and group files received together. section 20.3
  • Write each set to a date-named ZIP file when storage space matters. After writing each file, remove its working copy. section 20.4.1
Listing 20.1 File files_01.py
import datetime
import pathlib
 
FILE_PATTERN = "*.txt"                                        #1
ARCHIVE = "archive"
 
def main():
    
    date_string = datetime.date.today().strftime("%Y-%m-%d")  #2
 
    cur_path = pathlib.Path(".")
    archive_path = cur_path.joinpath(ARCHIVE)       
    archive_path.mkdir(exist_ok=True)                         #3
 
    paths = cur_path.glob(FILE_PATTERN)
 
    for path in paths:
        new_filename = f"{path.stem}_{date_string}{path.suffix}"
        new_path = archive_path.joinpath(
                                         new_filename)        #4
        path.rename(new_path)                                 #5
 
if __name__ == '__main__':
     main()

A runnable filename-based approach: `glob` selects matching text files; `stem` and `suffix` preserve each name’s parts while `rename` moves the dated result into `archive`. section 20.2

Listing 20.2 File files_02.py
import datetime
import pathlib
 
FILE_PATTERN = "*.txt"
ARCHIVE = "archive"
 
def main():
    
    date_string = datetime.date.today().strftime("%Y-%m-%d")
 
    cur_path = pathlib.Path(".")
 
    archive_path = cur_path.joinpath(ARCHIVE)
    archive_path.mkdir(exist_ok=True)                     #1
    new_path = archive_path.joinpath(date_string)
    new_path.mkdir(exist_ok=True)                         #2
 
    paths = cur_path.glob(FILE_PATTERN)
 
    for path in paths:
        path.rename(new_path.joinpath(path.name))
 
if __name__ == '__main__':
     main()

A small change groups each day’s files: create the dated directory first, then move each path into it using its existing name. section 20.3

Listing 20.3 File files_03.py
import datetime
import pathlib
import zipfile                                          #1
 
FILE_PATTERN = "*.txt"
ARCHIVE = "archive"
 
def main():
    
    date_string = datetime.date.today().strftime("%Y-%m-%d")
 
    cur_path = pathlib.Path(".")
    archive_path = cur_path.joinpath(ARCHIVE)
    archive_path.mkdir(exist_ok=True)
    
    paths = cur_path.glob(FILE_PATTERN)
 
    zip_file_path = cur_path.joinpath(ARCHIVE, 
                           date_string + ".zip")        #2
    zip_file = zipfile.ZipFile(str(zip_file_path), 
                                  "w")                  #3
 
    for path in paths:
        zip_file.write(str(path))                       #4
        path.unlink()                                   #5
 
if __name__ == '__main__':
     main()

The ZIP variant creates a dated archive with `zipfile.ZipFile`, writes each matching file, and calls `unlink` to clear its working copy. section 20.4.1

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through three runnable procedures: dated filenames, dated directories, and ZIP files; notes organization and storage trade-offs.
Examples
Uses a daily three-file product feed and scripts producing dated filenames, dated folders, or dated ZIP files.
What it assumes
Assumes basic Python scripting while demonstrating the path operations and ZIP calls needed for each approach.
What you can do after
Build it from scratch with Python’s pathlib, datetime, and zipfile modules to archive recurring file sets.
How it connects
Connects file archiving to pathlib paths, glob matching, file renaming, directory creation, ZIP compression, and file deletion.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.