永发信息网

python 怎样或读取一个文件的最后一行

答案:2  悬赏:20  手机版
解决时间 2021-04-01 19:33
  • 提问者网友:战魂
  • 2021-04-01 14:48
python 怎样或读取一个文件的最后一行
最佳答案
  • 五星知识达人网友:掌灯师
  • 2021-04-01 15:21
有两种情况,
1,文件比较大时,一行一行循环直到最后一行,读取最后一行;

targetLine = "";
lineNo = 0;  
while 1:
    mLine = file.readline();
    if not mLine:
        break;
    lineNo += 1;
    if (linecount == lineNO):
        targetLine = mLine;2, 文件比较小,直接读取全文,取最后一行数据。

targetLine = "";  
mLines = file.read();
targetLine = mLines[-1];filelineno( ) 
Return the line number in the current file. Before the first line has been read, returns 0. After the last line of the last file has been read, returns the line number of that line within the file.
全部回答
  • 1楼网友:鱼芗
  • 2021-04-01 16:01
有两种情况,
1,文件比较大。
targetLine = "";
lineNo = 0;
while 1:
mLine = file.readline();
if not mLine:
break;
lineNo += 1;
if (linecount == lineNO):
targetLine = mLine;
2, 文件比较小
targetLine = "";
mLines = file.read();
targetLine = mLines[-1];
filelineno( )
Return the line number in the current file. Before the first line has been read, returns 0. After the last line of the last file has been read, returns the line number of that line within the file.
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯