Created
April 17, 2021 02:25
-
-
Save thepurpleowl/0e4b3bda1cfa9ae41d31551f666735d6 to your computer and use it in GitHub Desktop.
Filter out files while retaining the directory structure: using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Written by thepurpleowl | |
# | |
# Filter out files while retaining the directory structure | |
import os | |
import json | |
from shutil import copyfile | |
import pathlib | |
# dev__manifest contains path information of unfiltered files | |
# this is optional | |
with open('dev__manifest.json') as f: | |
data = json.load(f) | |
# main logic | |
paths = [] | |
for filepath in data: | |
paths.append(filepath['filepath']) | |
#print(paths) | |
for currentpath, folders, files in os.walk('./<root_dir_path>'): | |
for file in files: | |
ufile = os.path.join(currentpath, file) | |
p = pathlib.Path(ufile) | |
upath = '/'.join(p.parts[1:]) | |
#print (upath) | |
if upath in paths: | |
dstroot = os.path.join(os.getcwd(), '<new_root_dir_path>') | |
dstdir = os.path.join(dstroot, os.path.dirname(upath)) | |
try: | |
os.makedirs(dstdir) | |
except: | |
pass | |
copyfile(ufile, os.path.join(dstdir,file)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment