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
