import math i=0 filename='' write='' Rline = '' def writeFile(fileName,line): global filename global write global Rline if filename!=fileName and filename=='': write = open('D:/soft/apache-tomcat-7.0.47/logs/'+fileName+".log", 'a+') write.write(line) elif filename!=fileName and Rline!='': write.close()#新文件就关闭上个write的流 else: write.write(line) filename = fileName Rline = line with open('D:/soft/apache-tomcat-7.0.47/logs/cloudq.log','r') as infile: for line in infile: name = i/500#设置每一个文件500行。 # print(math.floor(name)) writeFile(str(math.floor(name)),line) i=i+1 如果代码报错,请去掉中文注释再试。