python在windows下實現備份程式執行個體_python

來源:互聯網
上載者:User

很多書籍裡面講的Python備份都是在linux下的,而在xp上測試一下也可以執行備份功能,代碼都差不多相同,就是到執行打包的時候是不一樣的。而且要用到winrar,其他的壓縮檔也是一樣的。

首先我們要把winrar的路徑添加到path裡面,這裡添加完了要重啟機子才有效。
這裡要注意:把winrar的路徑添加到path裡面之後一定要重啟,否則path的設定不會起作用,打包就會失敗!
 
這裡用到得命令是:winrar a xxx.zip xxxx
xxx為任一字元
 
執行個體代碼如下:

#備份指令碼,用來備份的 #Filename:backup_ver1.py import os import time import sys #備份的源檔案路徑 sourc = ['G://test//test.txt'] #備份的檔案所放的地方 target_dir = 'G://' #備份檔案的名字 target = target_dir + time.strftime('%Y%m%d%H%M%S')+'.rar' #zip_command = "zip -qr '%s' %s" % (target,''.join(sourc)) #zip_command = "winrar a /"%s/" %s" % (target,' '.join(sourc)) zip_command="winrar a %s %s" %(target,' '.join(sourc)) print zip_command if os.system(zip_command) == 0:   print '打包成功!'+target else:   print '打包失敗!

聯繫我們

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