Advertisement
Guest User

getallfiles.py

a guest
Dec 6th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. '''
  2. getallfiles.py by Daniel Admon
  3.  
  4. finds all files in {dir} directory + subdirectories
  5. and move them to {newdir}
  6.  
  7. '''
  8. import os
  9.  
  10. dir = "D:\\Backup\Y"
  11. newdir = "D:\\Backup\X";
  12.  
  13. # -- more options:
  14. #dir = os.getcwd()
  15. #dir = input("Please type path to the folder:  ")
  16.  
  17. if not os.path.exists(dir):
  18.     print(f"Error: directory \"{dir}\" doesn't exist.")
  19.     exit()
  20.  
  21. if not os.path.exists(newdir):
  22.     os.makedirs(newdir)
  23.  
  24. count = 0
  25. for path, subdirs, files in os.walk(dir):
  26.     for name in files:
  27.         check = os.path.join(path, name)
  28.         movedfile = os.path.join(newdir, name)
  29.         if os.path.isfile(check):
  30.             try:
  31.                 os.rename(check, movedfile)
  32.             except IOError as error:
  33.                 print(error)
  34.             except:
  35.                 print('An error occured.')
  36.             else:
  37.                 print(f"File: {check} moved to: {newdir}")
  38.                 count += 1
  39.  
  40.  
  41. print(f"total moved: {count}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement