问题描述
我想为我的一个程序IN PYTHON实现一个命令行进度条,该程序逐行从文件中读取文本。
I would like to implement a command line progress bar for one of my programs IN PYTHON which reads text from a file line by line.
我可以采用以下两种方式之一来实现进度表:
I can implement the progress scale in one of two ways:
- (行数/总行数)或
- (已完成的字节数/总字节数)
我不在乎,但是行数
似乎需要我遍历整个文档(可能很大)才能获取值总行数
。
I don't care which, but "number of lines"
would seem to require me to loop through the entire document (which could be VERY large) just to get the value for "total lines"
.
这似乎效率很低。我在盒子外面思考,也许想想如果我考虑文件的大小(更容易获取?)并跟踪已读取的字节数,那么它可能会成为良好的进度条指标。
This seems extremely inefficient. I was thinking outside the box and thought perhaps if I took the size of the file (easier to get?) and kept track of the number of bytes that have been read, it might make for a good progress bar metric.
我可以使用 os.path.getsize(file)
或 os.stat( file).st_size
来检索文件的大小,但是我还没有找到一种方法来跟踪 readline()。我正在使用的文件应该以ASCII甚至是Unicode编码,所以...我应该确定使用的编码,然后记录读取的字符数或使用
os.getsizeof()
或某些 len()
函数用于读取的每一行?
I can use
os.path.getsize(file)
or os.stat(file).st_size
to retrieve the size of the file, but I have not yet found a way to keep track of the number of bytes read by readline()
. The files I am working with should be encoded in ASCII, or maybe even Unicode, so... should I just determine the encoding used and then record the number of characters read or use os.getsizeof()
or some len()
function for each line read?
我确信这里会出现问题。有什么建议吗?
I am sure there will be problems here. Any suggestions?
(PS-我认为手动输入一次读取的字节数不会起作用,因为我需要单独处理每一行;否则,我以后需要将其拆分为 n。)
(P.S. - I don't think manually inputting the number of bytes to read at a time will work, because I need to work with each line individually; or else I will need to split it up afterwards by "\n"'s.)
推荐答案
bytesread = 0
while True:
line = fh.readline()
if line == '':
break
bytesread += len(line)
或者,短一点:$ b $ b
Or, a little shorter:
bytesread = 0
for line in fh:
bytesread += len(line)
使用
os.path.getsize()
(或 os.stat
)是确定文件大小的有效方法。
Using
os.path.getsize()
(or os.stat
) is an efficient way of determining the file size.
这篇关于跟踪读取的字节数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!