Python 3 and progress indicator queries

I am trying to upload files using python queries. It worked in python 2.7, but not now. I am really confused and there should be a simpler answer. Since files can be quite large, I really want a progress bar, and I use presson procressbar to do the job.

r = requests.get(file_url, data={'track': 'requests'}) size = int(r.headers['Content-Length'].strip()) self.bytes = 0 widgets = [name, ": ", Bar(marker="|", left="[", right=" "), Percentage(), " ", FileTransferSpeed(), "] ", self, " of {0}MB".format(round(size / 1024 / 1024, 2))] pbar = ProgressBar(widgets=widgets, maxval=size) pbar.start() file = b"" for chunk in r.iter_content() if chunk: file += chunk self.bytes += 1 pbar.update(self.bytes) 

I found that using iter_content was the best way to get continuous updates. I tried iter_lines, but it messed up the files. It stops loading suddenly and is very slow, it takes 15 minutes to load 10%, after which it stops. And an attempt to open the file in byte mode, and writing to it does not work, it does not cause an error at all. And when I try to print what the piece contains, using

 print(chunk.decode("utf-8") 

It works, but only a few characters. At one point he complains about

 UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte 

Even using "decode_unicode = True" in iter_content does nothing. I'm at a standstill and don’t know what to do. It should not be difficult to use Py3k.

+4
source share
1 answer

I managed to fix it. So here is the updated code snippet:

 r = requests.get(file_url) size = int(r.headers['Content-Length'].strip()) self.bytes = 0 widgets = [name, ": ", Bar(marker="|", left="[", right=" "), Percentage(), " ", FileTransferSpeed(), "] ", self, " of {0}MB".format(str(round(size / 1024 / 1024, 2))[:4])] pbar = ProgressBar(widgets=widgets, maxval=size).start() file = [] for buf in r.iter_content(1024): if buf: file.append(buf) self.bytes += len(buf) pbar.update(self.bytes) pbar.finish() 

Download speed has changed from 7 kbps to 400+ kbps. And it works completely.

+4
source

Source: https://habr.com/ru/post/1411656/


All Articles