In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Python to display file download progress bar". In daily operation, I believe many people have doubts about how to use Python to display file download progress bar. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use Python to display file download progress bar". Next, please follow the editor to study!
1. Preface
When you use Python to write some Mini Program, you will often use files to download, for some smaller files, you may not care too much about the progress of the file downloads, because the download will be finished soon.
But when the file is large, such as downloading chromedriver, it would be friendly if we could see the download progress bar. After all, there is a similar progress bar when npm,pip installs the package.
2 、 requests
The requests library is believed to have been used by everyone, and it is necessary for interface testing. In fact, we usually download files, we can also use requests to do this, for example, there is such an address: https://autoupdate.termius.com/windows/Termius.exe
What should we do if we want to download it?
Here to write pseudo code, very easy to understand.
# step 1: visit the link import requestsr = requests.get ("https://autoupdate.termius.com/windows/Termius.exe")# step 2: get the contents of the returned file and write to the local with open (r". / termius.exe "," wb ") as f: f.write (r.content)
As my file is very large, so the process is very long, I will not show the details here. You can find a few M small file to try. Put the final result:
As you can see, after the execution, the file is also obtained.
3. Thinking
Do you think that downloading files in this way is very monotonous, and I don't know the progress, such as how many files have been downloaded, especially for larger files, waiting like this. I thought he was disconnected.
If only we could show the progress bar when downloading the file, such as Xunlei / Baidu network disk.
Only one library needs to be installed
To do this, all we need to do is install the tqdm library.
Pip install tqdm
Here is the sample code with comments:
Import requestsfrom tqdm import tqdmdef download (url: str, fname: str): # get the url data by stream stream resp = requests.get (url, stream=True) # get the length of the file and initialize the total to 0 total = int (resp.headers.get ('content-length', 0)) # Open the fname file in the current directory (name you pass in) # initialize tqdm, pass in the total number, file name and other data Then it is written. Update and other operations with open (fname, 'wb') as file, tqdm (desc=fname, total=total, unit='iB', unit_scale=True, unit_divisor=1024) ) as bar: for data in resp.iter_content (chunk_size=1024): size= file.write (data) bar.update (size) if _ _ name__ = "_ _ main__": # download file Pass in the file name url = "https://autoupdate.termius.com/windows/Termius.exe" download (" https://autoupdate.termius.com/windows/Termius.exe", ".exe")
The code is relatively simple, the content of tqdm is basically fixed writing, we do not have to doubt, pay attention to requests and other familiar parts.
Let's take a look at the gif effect and see if it is awesome (don't wonder why my cmd is so handsome, I won't tell you unless you like it):
At this point, the study on "how to use Python to display the file download progress bar" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.