python文件输出不完整。
巴扎黑
巴扎黑 2017-04-17 17:39:21
0
3
410

刚刚学python,小白一枚,今天拿python来去除一段代码前面的行号,
待处理文件看起来是这样的
001package org.apche.lucene.demo;
002import java.io.BufferedReader;

python代码是这么写的(在ipython环境中)

f = open("in")
lines = f.readlines()
fout = open("out", "w")
for line in lines:
    fout.write(line[3:])
exit()

结果输出文件当中只有117行代码,而输入文件中明明有239行。
不是很懂其中的原因,考虑过缓冲区未写出的问题,但是exit()之后输出文件仍然只有117行代码。求大神指点,多谢!

巴扎黑
巴扎黑

reply all(3)
刘奇

Guess: There is a character after line 117 that allows readline() to determine the end of the file

You can add a breakpoint and try it step by step.

迷茫

You can check len(lines) to see if it is 239.

And I suggest you add

fout.close()
f.close()
黄舟

Maybe it’s because in this file, there are more than 100 lines of text with a length less than 3

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template