2021年10月18日14:15:22 星期一
python 文件操作函数 readlines 和 tell 例子
示例代码 test.py
import sys
if __name__ == '__main__':
fn = str(sys.argv[1])
with open(fn, 'r+') as f:
print "file name: ", f.name
print("current postion is "+str(f.tell()))
line_count = len(f.readlines())
print("line_count is "+str(line_count))
print("current postion is "+str(f.tell()))
data.txt
cat data.txt
1 a
2 b
3 c
4 d
5 e
6 f
验证
$ python test.py data.txt
file name: data.txt
current postion is 0
line_count is 6
current postion is 23
网友评论