输入参数:
首先引入sys,
然后通过sys.argv[]来访问
示例代码:
import sys
print "argument number is "+str(len(sys.argv))
for i in range(0,len(sys.argv),1):
print "argument "+str(i)+" : "+sys.argv[i]
文件读写:
inf = open("in.txt","rb")
outf = open("out.txt","w")
count = 0
for line in inf.readlines():
newline = str(count)+":"+line
outf.write(newline)
count +=1
inf.close()
outf.close()
遍历文件:
遍历文件夹下的文件
import os
indir ="./get_sample_match_road"
for parent,dirnames,filenames in os.walk(indir):
print "parent dir:"
print "\t"+parent
print "directory name:"
for dirname in dirnames:
print "\t"+dirname
print "file name:"
for filename in filenames:
print "\t"+filename
print "*************************"
字典的使用:
#define
dict = {}
#insert
dict["sogou"] = 1
dict["baidu"] = 2
dict["gaode"] = 3
#judge if has a key:
print dict.has_key("baidu")
print dict.has_key("tengxun")
#get a val by key:
print dict["baidu"]
#travel the dict:
for k in dict:
print "dict[%s]=%s" % (k,dict[k])