[Python] 备份文件

Note:
1. windows下面的文件"斜杠和反斜杠":
使用"\\"代替"\"
2. zip 命令传入参数不需要加引号
3. 通过cmd测试zip命令是否可以通过命令行使用
4. cmd输入命令格式应与os.system('')内容一致



#!/usr/bin/python
# Filename: backup_ver1.py

import os
import time

source =[r'E:\code\python']
target_dir = 'E:\\code\\python\\' # Remember to change this to what you will be using
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'
zip_command = "zip -qr %s %s" % (target, ' '.join(source)) # !!!!!! %s 不需要加引号!!!!!!!

# Run the backup
if os.system(zip_command) == 0:
    print 'Successful backup to', target
else:
    print os.system(zip_command)
    print 'Backup FAILED'
 

posted @ 2018-01-13 17:39  开心泡泡龙  阅读(68)  评论(0)    收藏  举报