python 怎样或读取一个文件的最后一行
有没有简便的方法,获取文件的行数我用了这个方法,importosfile=open('abc.dat','r')linecount=len(file.readlines(...
有没有简便的方法,获取文件的行数
我用了这个方法,
import os
file = open('abc.dat','r')
linecount = len(file.readlines());
targetLine = "";
lineNo = 0;
while 1:
mLine = file.readline();
if not mLine:
break;
lineNo += 1;
if (linecount == lineNO):
targetLine = mLine;
但是最后发现targetLine里面是空的 展开
我用了这个方法,
import os
file = open('abc.dat','r')
linecount = len(file.readlines());
targetLine = "";
lineNo = 0;
while 1:
mLine = file.readline();
if not mLine:
break;
lineNo += 1;
if (linecount == lineNO):
targetLine = mLine;
但是最后发现targetLine里面是空的 展开
展开全部
有两种情况,
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,文件比较大。
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,文件比较大。
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.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询