哪里有带答案的 Python 习题
1,使用getopt。getopt()优化当前的功能函数:
[html]
#!/usr/bin/python
# -*- coding: utf-8 -*-
#coding=utf-8
import os,sys
import getopt
print sys.argv
CDROW='/home/zhouqian/test'
def cdWalker(CDROW,cdfile):
result=[]
for root,dirs,files in os.walk(CDROW):
result.append("%s %s %s" %(root,dirs,files))
print root
open(cdfile,'w').write('\n'.join(result))
def usage():
print '''pycdc 使用方式:
python cdays-3-exercise-1.py -d cdc -k 中国火
#检索cdc中有没有中国火字样的目录,
'''
try:
opts,args=getopt.getopt(sys.argv[1:],'hd:e:k:')
except getopt.GetoptError:
usage()
sys.exit()