Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for eachFile in glob(args.path + "/*/*.json"): <- this seems dangerous. Better way?
- # do something to the json file
- # output the modified data to its new home
- #outfile = os.path.join(args.output, os.path.dirname(eachFile), eachFile) <- doesn't work
- outfile = os.path.join(args.putout, os.path.dirname(eachFile)[1:], eachFile)
- # Recurse and process files.
- import os
- import sys
- from fnmatch import fnmatch
- import shutil
- def process(src_dir, dst_dir, pattern='*'):
- """Iterate through src_dir, processing all files that match pattern and
- store them, including their parent directories in dst_dir.
- """
- assert src_dir != dst_dir, 'Source and destination dir must differ.'
- for dirpath, dirnames, filenames in os.walk(src_dir):
- # Filter out files that match pattern only.
- filenames = filter(lambda fname: fnmatch(fname, pattern), filenames)
- if filenames:
- dir_ = os.path.join(dst_dir, dirpath)
- os.makedirs(dir_)
- for fname in filenames:
- in_fname = os.path.join(dirpath, fname)
- out_fname = os.path.join(dir_, fname)
- # At this point, the destination directory is created and you
- # have a valid input / output filename, so you'd call your
- # function to process these files. I just copy them :D
- shutil.copyfile(in_fname, out_fname)
- if __name__ == '__main__':
- process(sys.argv[1], sys.argv[2], '*.txt')
Add Comment
Please, Sign In to add comment