ift_backup.py 823 B

12345678910111213141516171819202122232425262728
  1. import os
  2. from pathlib import Path
  3. def ift_backup(base_dir=None):
  4. if base_dir is None:
  5. base_dir = 'E:\\IFT'
  6. source_path = Path(base_dir) / 'prod'
  7. target_path = base_dir + '/archive'
  8. for source_file in source_path.glob('*'):
  9. # print(source_file)
  10. file_temp = source_file.name
  11. if file_temp.count('_') == 2:
  12. prefix, filetype, timestamp = file_temp.split('_')
  13. year = timestamp[:4]
  14. month = timestamp[4:6]
  15. target = Path(f"{target_path}/{year}/{filetype}/{year}-{month}/{source_file.name}").absolute()
  16. os.makedirs(target.parent, exist_ok=True)
  17. if target.exists():
  18. target.unlink()
  19. print(target)
  20. source_file.rename(target)
  21. if __name__ == '__main__':
  22. ift_backup()