对于同一文件,您可以同时具有两个文件对象(一个用于读取,一个用于写入):
def removeLine(filename, lineno): fro = open(filename, "rb") current_line = 0 while current_line < lineno: fro.readline() current_line += 1 seekpoint = fro.tell() frw = open(filename, "r+b") frw.seek(seekpoint, 0) # read the line we want to discard fro.readline() # now move the rest of the lines in the file # one line back chars = fro.readline() while chars: frw.writelines(chars) chars = fro.readline() fro.close() frw.truncate() frw.close()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)