python--文件处理范例
import os,os.path,string dir="D:\\curl\\data" if (os.path.exists(dir)==False): print "not exist,create" long_time=[] def func(a): if a >=10: return True else: return False def func2(a): if len(a.strip())==0: return -1 b=a.rstrip('\n') c=string.atof(b) return c os.chdir(dir) for file in os.listdir(dir): #print file with open(file,'r') as f: content = f.readlines() if content!= []: data=map(func2,content) long=filter(func,data) if long!=[]: long_time.append(long) f.close() with open(r"d:/curl/long_time.txt",'w') as f: print long_time for data in long_time: f.write(str(data[0])) f.write("\n")
读取某个文件夹下所有文件