python学习之课后习题
2012-06-27 20:42
302 查看
编写一个动态备份文件的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
相关文章推荐
- 学习python核心编程-第三章-课后习题笔记
- 【鱼C·零基础入门学习Python】课后习题答案 第14课
- python学习之课后习题
- Python学习笔记: 第五章课后习题练习
- coursera 吴恩达 -- 第一课 神经网络和深度学习 :第二周课后习题 Python Basics with numpy (optional)
- 《linux c 编程一站式学习》课后部分习题解答
- Python学习_06 习题练习
- python学习第六章字典部分课后练习自己尝试的代码
- 笨方法学习Python-习题13: 参数、解包、变量
- Python 从入门到实践 7-4 课后习题
- 笨方法学习Python-习题19: 函数和变量
- 《linux c 编程一站式学习》课后部分习题解答
- Python 从入门到实践 5-1 课后习题
- 黑马程序员---Objective-C基础学习---一道课后习题引发的思考
- Python课后习题-第八章 读写文件
- coursera 吴恩达 -- 第一课 神经网络和深度学习 :第三周课后习题 Key concepts on Deep Neural Networks Quiz, 10 questions
- 零基础入门学习Python习题3【输密码+水仙花数+摸三色球】
- IOS学习---课后习题1
- 笨方法学习Python-习题12:提示别人
- Python 从入门到实践 8-3 课后习题