python学习之课后习题

10 篇文章 0 订阅

编写一个动态备份文件的python脚本:

#!/usr/bin/python
#filename :backup_ver2.py
import os
import time
import sys
source=[]
for i in sys.argv:
        print i
        source.append(i)
#source=['/root/python/test.py','/root/python/backup_var1.py']
print source
del(source[0])
print '#######'
print source
target_dir='/root/python/'
today=target_dir+time.strftime('%Y%m%d')
now=time.strftime('%H%M%S')
if not os.path.exists(today):
        os.mkdir(today)
        print 'successfully created directory', today
target=today+os.sep+now+'.zip'

zip_command="zip -qr '%s' %s" %(target,' '.join(source))
#run the backup
if os.system(zip_command)==0:
        print 'successfully backup to the target',target
else:
        print 'failed backup'
这里利用list的可增可减的特性,进过反复的实验,发现使用sys.argv的方法后会把本身的py脚本也包裹到里面如下:

[root@fsailing1 python]# python backup_ver2.py /root/python/test.py /root/python/while.py
backup_ver2.py
/root/python/test.py
/root/python/while.py
['backup_ver2.py', '/root/python/test.py', '/root/python/while.py']
successfully backup to the target /root/python/20120627/203320.zip
所以使用了del的方法进行删除了,这样就会好点儿了。

[root@fsailing1 python]# python backup_ver2.py /root/python/test.py /root/python/while.py
backup_ver2.py
/root/python/test.py
/root/python/while.py
['backup_ver2.py', '/root/python/test.py', '/root/python/while.py']
#######
['/root/python/test.py', '/root/python/while.py']
successfully backup to the target /root/python/20120627/203634.zip




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值