1. 查看文本中的单行是否是空行
getContent = open(fileName) for each_line in getContent: if line_content.strip()=='': print("this is empty line") else: print(each_line) getContent.close()
2. 替换文件中的指定字符
def text_replace(fileName,pldText,newText): f1 = open(fileName,'r+') infos = f1.readlines() f1.seek(0,0) for line in infos: line_new = line.replace(pldText,newText) f1.write(line_new) f1.close()