home ~ socials ~ projects ~ rss

Create a List of File Update Paths in Python

October 2025

This is how I create a list of input files to make updates to and their corresponding output paths in an _updates directory.

The idea is that you the new directory of files gets built with the same structure so you can move it in place when everything is done properly.

import os
from pathlib import Path

def build_update_list(dir, extensions):
    file_list = []
    for root, dirs, files in os.walk(dir):
        for file in files:
            in_file = os.path.join(root, file)
            ext = "".join(Path(in_file).suffixes)
            if ext in extensions:
                out_file = os.path.join("_updates", root, file)
                file_list.append((in_file, out_file))
    return file_list

if __name__ == "__main__":
  update_list = build_update_list("recursive_test", [".txt"])
  for paths in update_list:
    print(f"IN:  {paths[0]}")
    print(f"OUT: {paths[1]}\n")
Output:
IN:  recursive_test/1.txt
OUT: _updates/recursive_test/1.txt

IN:  recursive_test/a/2.txt
OUT: _updates/recursive_test/a/2.txt

IN:  recursive_test/a/c/4.txt
OUT: _updates/recursive_test/a/c/4.txt

IN:  recursive_test/b/3.txt
OUT: _updates/recursive_test/b/3.txt
end of line
Share link:
https://www.alanwsmith.com/en/34/gl/gz/vu/?create-a-list-of-file-update-paths-in-python