簡明 Python 教程中的第一個備份腳本

第一次學習python寫的腳本t_0015.gif  

原爲簡明 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 包括子目錄及子目錄文件)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章