原为简明 Python 教程中的第一个脚本 原脚本如下
#!/usr/bin/python # Filename: backup_ver1.py import os import time # 1. The files and directories to be backed up are specified in a list. source = [‘/home/swaroop/byte‘, ‘/home/swaroop/bin‘] # If you are using Windows, use source = [r‘C:\Documents‘, r‘D:\Work‘] or something like that # 2. The backup must be stored in a main backup directory target_dir = ‘/mnt/e/backup/‘ # Remember to change this to what you will be using # 3. The files are backed up into a zip file. # 4. The name of the zip archive is the current date and time target = target_dir + time.strftime(‘%Y%m%d%H%M%S‘) + ‘.zip‘ # 5. We use the zip command (in Unix/Linux) to put the files in a zip archive zip_command = "zip -qr ‘%s‘ %s" % (target, ‘ ‘.join(source)) # Run the backup if os.system(zip_command) == 0: print ‘Successful backup to‘, target else: print ‘Backup FAILED‘
因为原脚本是Linux下的 所以个人修改为Windows下的脚本
#coding=utf-8 import os import time import sys reload(sys) sys.setdefaultencoding(‘gbk‘) source = ‘F:\\movie\\文字效果\\‘ target_dir = ‘F:\\backup\\‘ target = target_dir + time.strftime(‘%Y%m%d%H%M%S‘) + ‘.zip‘ un_source = unicode (source,‘utf8‘) zip_command = "7z a -tzip %s %s -r" % (target,un_source) print zip_command if os.system(zip_command) == 0: print ‘Successful backup to‘, target else: print ‘Backup FAILED‘
因为要备份的文件夹路径存在中文所以有以下的代码
import sys reload(sys) sys.setdefaultencoding(‘gbk‘) --------------------------------------------- unicode (source,‘utf8‘)
打包的压缩程序使用了7zip 调用为
7z a -tzip 打包到文件 需要打包的目录 -r(-r 包括子目录及子目录文件)
本文出自 “CANYU'S PYTHON” 博客,请务必保留此出处http://canyuexiang.blog.51cto.com/8871379/1556391
原文地址:http://canyuexiang.blog.51cto.com/8871379/1556391