python拷貝檔案夾中特定日期內的檔案,

來源:互聯網
上載者:User

python拷貝檔案夾中特定日期內的檔案,

需求:遍曆一個檔案夾,將其中修改日期在2015-02-01——2015-02-20之間的檔案拷貝出來,並且拷貝出來的檔案要保持原來的目錄結構。

實現代碼(python3.3):

# encoding: utf-8import osimport shutilimport timetime0 = "2015-02-01"#起始日期time1 = "2015-02-20"#結束日期targetDir = "D:\\game"#目標目錄print("正在處理,請稍等.....")curDir = os.getcwd()for root, dirs, files in os.walk(curDir):    #先建立目標目錄    curFolder = root[len(curDir)+1:]                    #提取當前檔案夾    tempTargetDir = os.path.join(targetDir, curFolder)  #產生目標目錄絕對路徑    # print(tempTargetDir)    if os.path.exists(tempTargetDir):        shutil.rmtree(tempTargetDir)    #再拷貝檔案    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("正在拷貝檔案:", absFileName)            shutil.copy(absFileName, tempTargetDir)os.system("pause")


使用:將檔案放到目標檔案夾,雙擊即可。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.