python備份檔案

來源:互聯網
上載者:User
任務:

你想對某個分類樹中的被修改過的檔案多次備份,以防止某次修改意外地抹去了你的編輯結果。 周期性的執行以下python指令碼可以對指定目錄下的檔案進行備份。

#-*- coding:utf-8 -*-import sys,os,shutil,filecmpMAXVERSIONS = 100def backup(tree_top, bakdir_name="bakdir"):for dir,subdirs,files in os.walk(tree_top):#確保每個目錄都有一個備份目錄backup_dir = os.path.join(dir,bakdir_name)if not os.path.exists(backup_dir):os.makedirs(backup_dir)#停止對備份目錄的遞迴subdirs[:] = [d for d in subdirs if d != bakdir_name]for file in files:filepath = os.path.join(dir,file)destpath = os.path.join(backup_dir,file)#檢查以前的版本是否存在for index in xrange(MAXVERSIONS):backfile = '%s.%2.2d' % (destpath, index)if not os.path.exists(backfile):breakif index > 0:old_backup = '%s.%2.2d' % (destpath,index-1)abspath = os.path.abspath(filepath)try:if os.path.isfile(old_backup) and filecmp.cmp(abspath, old_backup,shallow=False):continueexcept OSError:passtry:shutil.copy(filepath,backfile)except OSError:passif __name__ == '__main__':try:tree_top = sys.argv[1]except IndexError:tree_top = '.'backup(tree_top)

如果想針對某個特定尾碼名的檔案進行備份,(或對除去某個副檔名之外的檔案進行備份);在 for file in files 迴圈內加一個適當的測試即可:

for file in files:    name,ext = os.path.splitext(file)    if ext not in ('.py','.txt','.doc'):        continue

注意以下代碼,避免os.walk遞迴到要備份的子目錄。當os.walk迭代開始之後,os.walk根據subdirs來進行子目錄迭代。這個關於os.walk的細節也是產生器應用的一個絕佳例子,它示範了產生器是如何通過迭代的代碼擷取資訊,同時又是如何反過來影響迭代。

subdirs[:] = [d for d in subdirs if d != bakdir_name]
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.