import os
from io import BufferedReader, FileIO
class ProgressFile(BufferedReader):
# For binary opening only
def __init__(self, filename, read_callback):
f = FileIO(file=filename, mode='r')
self._read_callback = read_callback
super().__init__(raw=f)
# I prefer Pathlib but this should still support 2.x
self.length = os.stat(filename).st_size
def read(self, size=None):
calc_sz = size
if not calc_sz:
calc_sz = self.length - self.tell()
self._read_callback(position=self.tell(), read_size=calc_sz, total=self.length)
return super(ProgressFile, self).read(size)
def my_callback(position, read_size, total):
# Write your own callback. You could convert the absolute values to percentages
# Using .format rather than f'' for compatibility
print("position: {position}, read_size: {read_size}, total: {total}".format(position=position,
read_size=read_size,
total=total))
myfile = ProgressFile(filename='mybigfile.txt', read_callback=my_callback)
Azure库似乎没有包含用于监视进度的回调函数
幸运的是,您可以在Python的文件对象周围添加一个包装器,它可以在每次读取时调用回调
试试这个:
那你会做的
编辑: TQM(进度监视器)似乎有一个整洁的包装:https://github.com/tqdm/tqdm#hooks-and-callbacks。 这样做的好处是,您可以轻松访问一个相当不错的进度条
相关问题 更多 >
编程相关推荐