多くの本では Linux での Python バックアップについて説明されており、XP でテストするとバックアップ機能も実行できます。コードはほぼ同じですが、パッケージが異なります。また、winrar を使用する必要があります。他の圧縮ファイルについても同様です。
まず、winrar のパスをパスに追加する必要があります。追加した後は、マシンを再起動する必要があります。
ここで注意してください: winrar パスをパスに追加した後は必ず再起動してください。そうしないと、パス設定が有効にならず、パッケージ化が失敗します。
ここで使用されるコマンドは次のとおりです: winrar a xxx.zip xxxx
xxx は任意の文字
コード例は次のとおりです:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #备份脚本,用来备份的
#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 '打包失败!
|
ログイン後にコピー