因为我的机器里没有zip命令,但是我安装有WinRAR,所以用的rar命令:
安装WinRAR并配置环境变量Path (我的为C:\Program Files\WinRAR)
程序如下:
#!/usr/bin/python
# Filename:backup_ver1.py
import os
import time
source = [r"D:\pythonfile\f1", r"D:\pythonfile\f2"]
target_dir = "D:\\pythonfile\\f3\\"
target = target_dir+time.strftime("%Y%m%d%H%M%S")+".rar"
zip_command = "rar a %s %s" %(target, ' '.join(source))
#print(source)
#print(target_dir)
#print(target)
#print(zip_command)
if os.system(zip_command) == 0:
print("Successful backup to", target)
else:
print("Backup FAILED")
程序运行如图:
程序还有一问题:在命令行下运行是成功的,但是在IDLE下运行就是失败的!