Python copies objects within a specified date in a folder,

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.