在windows下用python腳本實現文件的備份,參考《A Byte of Python3》第十一章(Page59)。
#!/usr/bin/python # -*- coding: cp936 -*- import os import time source = ['E://'] # 待備份文件在E盤根目錄下 running = True while running: your_source = raw_input("Your own path or your own file path:") #如果使用input(),在運行后輸入路徑名時,需要在兩邊加上" ",下面的input同理 #比如欲備份E盤下zipme文件夾里的hello.txt文件,則應輸入zipme//hello.txt source.append(your_source) if raw_input("Do you want to add file or folder(y/n):")=='n': running = False target_dir = 'E://backup//' #備份生成的文件存放的路徑 #以當前日期和時間為文件名命名生成的壓縮文件 target = target_dir+/ time.strftime('%Y')+/ time.strftime('%m')+/ time.strftime('%d')+/ time.strftime('%H')+/ time.strftime('%M')+/ time.strftime('%S')+'.rar' #使用zip命令壓縮文件 zip_command = "zip -qr {0} {1}".format(target, ''.join(source)) #通過給系統傳遞參數來執行壓縮命令(壓縮使用的是WinRAR所帶文件rar.exe來執行壓縮) if os.system(zip_command) == 0: print('Successful backup to',target) else: print('Backup FAILED')
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答