Guest User

Untitled

a guest
May 21st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. for eachFile in glob(args.path + "/*/*.json"): <- this seems dangerous. Better way?
  2. # do something to the json file
  3.  
  4. # output the modified data to its new home
  5. #outfile = os.path.join(args.output, os.path.dirname(eachFile), eachFile) <- doesn't work
  6. outfile = os.path.join(args.putout, os.path.dirname(eachFile)[1:], eachFile)
  7.  
  8. # Recurse and process files.
  9. import os
  10. import sys
  11. from fnmatch import fnmatch
  12. import shutil
  13.  
  14.  
  15. def process(src_dir, dst_dir, pattern='*'):
  16. """Iterate through src_dir, processing all files that match pattern and
  17. store them, including their parent directories in dst_dir.
  18. """
  19. assert src_dir != dst_dir, 'Source and destination dir must differ.'
  20. for dirpath, dirnames, filenames in os.walk(src_dir):
  21. # Filter out files that match pattern only.
  22. filenames = filter(lambda fname: fnmatch(fname, pattern), filenames)
  23.  
  24. if filenames:
  25. dir_ = os.path.join(dst_dir, dirpath)
  26. os.makedirs(dir_)
  27. for fname in filenames:
  28. in_fname = os.path.join(dirpath, fname)
  29. out_fname = os.path.join(dir_, fname)
  30.  
  31. # At this point, the destination directory is created and you
  32. # have a valid input / output filename, so you'd call your
  33. # function to process these files. I just copy them :D
  34. shutil.copyfile(in_fname, out_fname)
  35.  
  36. if __name__ == '__main__':
  37. process(sys.argv[1], sys.argv[2], '*.txt')
Add Comment
Please, Sign In to add comment