参考自:http://zhidao.baidu.com/question/87467507.html //定义一个变量用来存读到的东西 string text = ""; //用一个读出流去读里面的数据 using (StreamReader reader = new StreamReader(@"C:\db.txt", Encoding.GetEncoding("gb2312"))) { //读一行 string line = reader.R
去除文件中重复的行 import os with open('db.txt','r',encoding='utf-8') as read_f,\ open('.db.txt.swap','w',encoding='utf-8') as write_f: s=set() for line in read_f: if line not in s: s.add(line) write_f.write(line) os.remove('db.txt') os.rename('.db.txt.swap',
def delblankline(infile,outfile): infopen = open(infile,'r') outfopen = open(outfile,'w') lines = infopen.readlines() for line in lines: if line.split(): outfopen.writelines(line) else: outfopen.writelines("") infopen.close() outfopen.close() de
希望是输入一个合法的文件夹的路径,然后代码自动读取该文件夹下的每个py结尾的文件内的代码行数,最后汇总一个数,但现在只是有思路,却没时间写,这是能读取同级文件下的某个文件, with open('test1.py', encoding='utf8') as f: count = 0 for line in f.readlines(): line = line.strip() # 去除每行的空格,每一个line都是字符串,去完空格方便下面的判断 if len(line) != 0 and line