#练习:subprocess模块来产生子进程 import subprocess obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) obj.stdin.write("print 1 ") obj.stdin.write("print 2 ") obj.stdin.write("print 3 ") obj.stdin.write("print uio ") obj.stdin.close() cmd_out = obj.stdout.read() obj.stdout.close() cmd_error = obj.stderr.read() obj.stderr.close() print cmd_out print cmd_error #练习:将错误码为200的access log找出来写到另一个文件中,写入的内容包括IP地址,时间,访问地址 import re def access_log(): try: with open("e:\test4\decode.txt","r") as f1: for line in f1.readlines(): result=re.search(r"s(200)s",line) if result: time=re.search(r"([.*])",line).group(1) resource=re.search(r"GET.*HTTP/1.1",line).group() with open("e:\test4\decode_result.txt","a+") as f2: f2.write(line[:15]+" "+time+" "+resource+" ") except Exception,e: print "error occurred" if __name__=="__main__": access_log() #练习:有一串字符串,比如string=”fjeuifhueifhyaf246438%&(+_(feufefuieadgd“,计算出每个字母出现了多少次,写出算法即可 1、创建一个字典dicts,key存字母,value存字母出现的次数,for循环这个string,判断if "某个字母".isalpha()并且dicts.has_key("某个字母"),则dicts["某个字母"]+=1,最后打印出dicts 2、for 循环string,用string.count("某个字母")计算某个字母出现的个数 3、正则:遍历string,用len(re.findall(r”某个字符”,string))求出这个字符对应的长度