Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. import os
  2. import shutil
  3. import time
  4. import errno
  5. import time
  6. import sys
  7. import logging
  8. import logging.config
  9.  
  10.  
  11.  
  12. source = r'C:UsersDesktopBetaSource'
  13. dest = r'C:UsersDesktopBetaDest'
  14. #Gets the current time from the time module
  15. now = time.time()
  16. #Timer of when to purge files
  17. cutoff = now - (14 * 86400)
  18. source_list = []
  19. all_sources = []
  20. all_dest_dirty = []
  21. logging.basicConfig(level = logging.INFO, filename = time.strftime("main-%Y-%m-%d.log"))
  22.  
  23. def main():
  24. dest_files()
  25. purge_files()
  26. #I used the dess_files function to get all of the destination files
  27. def dest_files():
  28. for dest_root, dest_subdirs, dest_files in os.walk(dest):
  29. for f in dest_files:
  30. global All_dest_dirty
  31. all_dest_dirty.append(f)
  32.  
  33. def purge_files():
  34. logging.info('invoke purge_files method')
  35. #I removed all duplicates from dest because cleaning up duplicates in dest is out of the scope
  36. all_dest_clean = list(dict.fromkeys(all_dest_dirty))
  37. #os.walk used to get all files in the source location
  38. for source_root, source_subdirs, source_files in os.walk(source):
  39. #looped through every file in source_files
  40. for f in source_files:
  41. #appending all_sources to get the application name from the file path
  42. all_sources.append(os.path.abspath(f).split('\')[-1])
  43. #looping through each element of all_source
  44. for i in all_sources:
  45. #logical check to see if file in the source folder exists in the destination folder
  46. if i not in all_dest_clean:
  47. #src is used to get the path of the source file this will be needed to move the file in shutil.move
  48. src = os.path.abspath(os.path.join(source_root, i))
  49. #the two variables used below are to get the creation time of the files
  50. t = os.stat(src)
  51. c = t.st_ctime
  52. #logical check to see if the file is older than the cutoff
  53. if c<cutoff:
  54. logging.info(f'File has been succesfully moved: {i}')
  55. print(f'File has been succesfully moved: {i}')
  56. shutil.move(src,dest)
  57. #removing the allready checked source files for the list this is also used in other spots within the loop
  58. all_sources.remove(i)
  59. else:
  60. logging.info(f'File is not older than 14 days: {i}')
  61. print(f'File is not older than 14 days: {i}')
  62. all_sources.remove(i)
  63. else:
  64. all_sources.remove(i)
  65. logging.info(f'File: {i} allready exists in the destination')
  66. print(f'File: {i} allready exists in the destination')
  67. if __name__ == '__main__':
  68. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement