Skip to content

Commit 24bd93b

Browse files
committed
added file downloader tutorial
1 parent c5a190f commit 24bd93b

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
3434
-[How to Make a Process Monitor in Python](https://www.thepythoncode.com/article/make-process-monitor-python). ([code](general/process-monitor))
3535
-[How to Make a Screen Recorder in Python](https://www.thepythoncode.com/article/make-screen-recorder-python). ([code](general/screen-recorder))
3636
-[How to Generate and Read QR Code in Python](https://www.thepythoncode.com/article/generate-read-qr-code-python). ([code](general/generating-reading-qrcode))
37+
-[How to Download Files in Python](https://www.thepythoncode.com/article/download-files-python). ([code](general/file-downloader))
3738

3839
-### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)
3940
-[How to Access Wikipedia in Python](https://www.thepythoncode.com/article/access-wikipedia-python). ([code](general/wikipedia-extractor))

‎general/file-downloader/README.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# [How to Download Files in Python](https://www.thepythoncode.com/article/download-files-python)
2+
To download a file using Python:
3+
-`pip3 install -r requirements.txt`
4+
-
5+
```
6+
python download.py https://download.winzip.com/gl/nkln/winzip24-downwz.exe
7+
```
8+
This will start downloading the file and **outputs** something like this:
9+
```
10+
Downloading winzip24-downwz.exe: 6%|█████▏ | 779k/11.8M [00:03<00:55, 210kB/s]
11+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
fromtqdmimporttqdm
2+
importrequests
3+
importsys
4+
5+
# the url of file you want to download, passed from command line arguments
6+
url=sys.argv[1]
7+
# read 1024 bytes every time
8+
buffer_size=1024
9+
# download the body of response by chunk, not immediately
10+
response=requests.get(url, stream=True)
11+
12+
# get the total file size
13+
file_size=int(response.headers.get("Content-Length", 0))
14+
15+
# get the file name
16+
filename=url.split("/")[-1]
17+
18+
# progress bar, changing the unit to bytes instead of iteration (default by tqdm)
19+
progress=tqdm(response.iter_content(buffer_size), f"Downloading {filename}", total=file_size, unit="B", unit_scale=True, unit_divisor=1024)
20+
withopen(filename, "wb") asf:
21+
fordatainprogress:
22+
# write data read to the file
23+
f.write(data)
24+
# update the progress bar manually
25+
progress.update(len(data))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tqdm
2+
requests

0 commit comments

Comments
(0)