Форум сайта python.su
0
Приветствую коллеги!
Большого опыта в языке пока нет, только практикуюсь.
Хочу прикрутить прогресс бар для работы с pycurl в webdavclient.
Нашел готовый код.
Почему-то не корректно отрабатывает sys.stdout.flush() и вывод на экран не сбрасывается а дополняется.
Пробовал на Python 2.7 и 3.6 система ubuntu 18.04
import pycurl import os, sys # pretty print progress and percentage completed def progress(total_to_download, total_downloaded, total_to_upload, total_uploaded): if total_to_upload: percent_completed = float(total_uploaded)/total_to_upload # You are calculating amount uploaded rate = round(percent_completed * 100, ndigits=2) # Convert the completed fraction to percentage completed = "#" * int(rate) # Calculate completed percentage spaces = " " * ( 100 - int(rate)) # Calculate remaining completed rate sys.stdout.write('[%s%s] %s%%' %(completed, spaces, rate)) # the pretty progress [#### ] 34% sys.stdout.flush() def upload_to_cloud(url, filename, is_proxy=False): if not os.path.exists(filename): raise Exception('did not find file') # initialize py curl c = pycurl.Curl() c.setopt(pycurl.UPLOAD, 1) if is_proxy: c.setopt(pycurl.PROXY, 'XXX') c.setopt(pycurl.PROXYPORT, 80) #For authenticated cloud store c.setopt(pycurl.USERPWD, 'login' + ':' + 'password') c.setopt(pycurl.READFUNCTION, open(filename, 'rb').read) c.setopt(pycurl.VERBOSE, 0) c.setopt(pycurl.URL, url) c.setopt(pycurl.NOPROGRESS, 0) c.setopt(pycurl.PROGRESSFUNCTION, progress) #Set size of the file to be uploaded filesize = os.path.getsize(filename) # you can simply open the file and do a byte counter for this. Initially that's what I did then moved to os API c.setopt(pycurl.INFILESIZE, filesize) # Start transfer print('Uploading file %s to url %s' %(filename, url)) c.perform() # this kicks off the pycurl module with all options set. c.close() if __name__=='__main__': if len(sys.argv) < 2: print('Usage python upload_to_cloud URL FILE_PATH') exit() upload_to_cloud(sys.argv[1], sys.argv[2])
Отредактировано tyler (Сен. 8, 2018 12:11:49)
Офлайн
0
Разобрался. Нужно добавить перевод каретки \r
Обсуждение link
sys.stdout.write('\r[%s%s] %s%%' %(completed, spaces, rate)) # the pretty progress [#### ] 34%
Отредактировано tyler (Сен. 8, 2018 13:58:10)
Офлайн