#Filename:backup_ver1
import os
import time
#1 Using list to specify the files and directory to be backed up
source = r'C:\Documents and Settings\rgolwalkar\Desktop\Desktop\Dr Py\Final_Py'
#2 define backup directory
destination = r'C:\Documents and Settings\rgolwalkar\Desktop\Desktop\PyDevResourse'
#3 Setting the backup name
targetBackup = destination + time.strftime('%Y%m%d%H%M%S') + '.rar'
rar_command = "rar.exe a -ag '%s' %s" % (targetBackup, ''.join(source))
##i am sure i am doing something wrong here - rar command please let me know
if os.system(rar_command) == 0:
print 'Successful backup to', targetBackup
else:
print 'Backup FAILED'
O/P:- Backup FAILED
WinRAR是下添加到環境變量PATH和CLASSPATH以及 - 任何人與備份目錄的建議是非常值得歡迎Python腳本備份目錄
好的,我這樣做(導入tarfile模塊),它運行安靜 - 我沒有給任何消息,因爲沒有任何東西添加產生一條消息 - 我檢查了位置destination = os.path.join(root,「文檔和設置「,」rgolwalkar「,」桌面「,」桌面「,」PyDevResourse「)但沒有找到備份。第二,如果我添加: - 如果os.system(tar)== 0:打印'成功備份到',targetBackup其他:打印'備份失敗' - 如果os.system(tar)== 1出現錯誤:TypeError:system()參數1必須是字符串,而不是TarFile – rgolwalkar
只是好奇,爲什麼你使用rar而不是跨平臺的東西,比如zip,它被內置到python中? –