自从大四第一学期实习之后,我就没再用过Python了,现在几乎忘光了-_-!。觉得还是得好好学习一门脚本;趁着最近比较闲,学习一下Python,两个小时翻看了<<A Byte Of Python>>1到10章,照着教材把第10章的例子最后面的优化自己实现了一下:
在命令行上面输入需要备份的文件或者目录,脚本自动进行备份,完全菜鸟级别的代码,贴出来当作复习功课。
代码
#
!/usr/bin/python
# Filename: backup4.py
import os
import sys
import time
# 传递备份文件或目录给脚本
source = sys.argv[ 1 :]
if len(source) == 0:
print ' usage: enter the files or directores '
sys.exit()
target_dir = ' /home/ken/backup_test/ '
today = target_dir + time.strftime( ' %Y%m%d ' )
now = time.strftime( ' %H%M%S ' )
if not os.path.exists(today):
os.mkdir(today)
print ' Successful create dir ' , today
commend = raw_input( ' Enter the commend: ' )
if len(commend) == 0:
target = today + os.sep + now + ' .zip '
else :
target = today + os.sep + now + ' _ ' + \
commend.replace( ' ' , ' _ ' ) + ' .zip '
bakcup_command = " tar -cvzf '%s' %s " % (target, ' ' .join(source))
if os.system(bakcup_command) == 0:
print ' Successful backup '
else :
print ' backup failed '
# Filename: backup4.py
import os
import sys
import time
# 传递备份文件或目录给脚本
source = sys.argv[ 1 :]
if len(source) == 0:
print ' usage: enter the files or directores '
sys.exit()
target_dir = ' /home/ken/backup_test/ '
today = target_dir + time.strftime( ' %Y%m%d ' )
now = time.strftime( ' %H%M%S ' )
if not os.path.exists(today):
os.mkdir(today)
print ' Successful create dir ' , today
commend = raw_input( ' Enter the commend: ' )
if len(commend) == 0:
target = today + os.sep + now + ' .zip '
else :
target = today + os.sep + now + ' _ ' + \
commend.replace( ' ' , ' _ ' ) + ' .zip '
bakcup_command = " tar -cvzf '%s' %s " % (target, ' ' .join(source))
if os.system(bakcup_command) == 0:
print ' Successful backup '
else :
print ' backup failed '
END