Python copies objects within a specified date in a folder,
Requirement: traverse a folder, copy the files modified between and, and keep the original directory structure.
Implementation Code (python3.3 ):
# Encoding: utf-8import osimport shutilimport timetime0 = "2015-02-01" # Start Date time1 = "2015-02-20" # End Date targetDir = "D: \ game" # target directory print ("processing, please wait ..... ") curDir = OS. getcwd () for root, dirs, files in OS. walk (curDir): # first create the target directory curFolder = root [len (curDir) + 1:] # extract the current folder tempTargetDir = OS. path. join (targetDir, curFolder) # generate the absolute path of the target directory # print (tempTargetDir) if OS. path. exists (tempTargetDir): shutil. rmtree (tempT ArgetDir) # copy the file t0 = time. mktime (time. strptime (time0, "% Y-% m-% d") t1 = time. mktime (time. strptime (time1, "% Y-% m-% d") + 3600*24 for fileName in files: absFileName = OS. path. join (root, fileName) if OS. path. splitext (absFileName) [1]! = '. Py' and OS. path. getmtime (absFileName)> = t0 and OS. path. getmtime (absFileName) <= t1: if not OS. path. exists (tempTargetDir): OS. makedirs (tempTargetDir) print ("copying file:", absFileName) shutil. copy (absFileName, tempTargetDir) OS. system ("pause ")
Use: Put the file in the target folder and double-click it.