Python:實現檔案歸檔

來源:互聯網
上載者:User

標籤:目錄   備份   

實現功能:

將E:\123檔案備份至E:\backup 檔案夾下,以當前的日期為子目錄存放備份後的檔案

1 #! /usr/bin/python 2 #Filename:backup.py 3 #功能說明:備份檔案,以當前日期為子目錄存放備份後的檔案 4  5 import os 6 import time 7 #要備份的目錄,可在此列表中增加 8 source = [r‘E:\123‘] 9 10 #備份檔案存放的目錄11 target_dir = ‘E:\\backup\\‘12 13 #取目前時間為備份子目錄名14 today = target_dir + time.strftime(‘%Y%m%d‘)15 now = time.strftime(‘%H%M%S‘)16 17 #在備份檔案名中加入注釋18 comment = input(‘Enter a comment:‘)19 if len(comment) == 0:20     target = today + os.sep + now + ‘.zip‘21 else:22     target = today + os.sep + now + ‘_‘ + 23 comment.replace(‘ ‘,‘_‘) + ‘.zip‘24 25 #如果目標目錄不存在就建立26 if not os.path.exists(today):27     os.mkdir(today)28     print (‘Sucessfully created directoy‘,today)29 30 #備份命令,可替換為7z,linux下可改為tar等31 zip_command = "winrar a %s %s"%(target, ‘ ‘.join(source))32 33 #執行命令34 if os.system(zip_command) == 0:35     print(‘Successful backup to‘,target)36 else:37     print(‘Backup failed‘)

注意:
pycharm運行出現報錯資訊如下:
"winrar" 不是內部或外部命令,也不是可啟動並執行程式或批次檔。
Backup failed

解決方案:
將winrar的安裝路徑,添加到環境變數path內即可。

Python:實現檔案歸檔

聯繫我們

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