歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python使用7z解壓軟件備份文件腳本分享

Python使用7z解壓軟件備份文件腳本分享

日期:2017/3/1 9:35:45   编辑:Linux編程

Python使用7z解壓軟件備份文件腳本。

要求安裝:
1.Python
2.7z解壓軟件

backup_2.py

# Filename: backup_2.py
#site www.linuxidc.com
'''Backup files.
Version: V2, based on Python 3.3
Usage: backup.py -s:"dir1|dir2|..." -t:"target_dir" [-c:"comment"]
-s: The source directories.
-t: The target directory.
-c: Optional, any comment.
Examples:
backup.py -s:"c:\\src\\F1|c:\\src\\F2|c:\\src\\F 3" -t:"c:\\backup"
backup.py -s:"c:\\src\\F 3" -t:"c:\\backup" -c:"For sample"'''
import os
import sys
import time
# Read sys.argv
print(sys.argv)
if len(sys.argv) < 2:
print(__doc__)
sys.exit()
source=[]
target_dir=''
comment=''
for arg in sys.argv:
if arg.startswith('-s:'):
source=arg[3:].split('|')
print(source)
elif arg.startswith('-t:'):
target_dir=arg[3:]+os.sep
print(target_dir)
elif arg.startswith('-c:'):
comment=arg[3:]
print(comment)
for i in range(0, len(source)):
source[i] = "\"" + source[i] + "\""
print(source[i])
# Make the file name with the time and comment
today=target_dir+time.strftime('%Y%m%d')
now=time.strftime('%H%M%S')
if len(comment)==0: # check if a comment was entered
target=today+os.sep+now+'.7z'
else:
target=today+os.sep+now+'_'+\
comment.replace(' ','_')+'.7z'
# Create the subdirectory by day
if not os.path.exists(today):
os.mkdir(today) # make directory
print('Successfully created directory',today)
# zip command
zip_command="7z a %s %s" %(target,' '.join(source))
print(zip_command)
# Run the backup
if os.system(zip_command)==0:
print('Successful backup to',target)
else:
print('Backup FAILED')

Python解析xml文檔實例 http://www.linuxidc.com/Linux/2012-02/54760.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved