-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloaderUsingCmdLine.py
30 lines (25 loc) · 1015 Bytes
/
DownloaderUsingCmdLine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import argparse
import requests
def download_file(url, local_filename):
if local_filename is None:
local_filename = url.split('/')[-1]
# NOTE the stream=True parameter below
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
# If you have chunk encoded response uncomment if and set chunk_size parameter to None.
#if chunk:
f.write(chunk)
return local_filename
parser = argparse.ArgumentParser()
# Add command line arguments
parser.add_argument("url", help="Url of the file to download")
# parser.add_argument("output", help="by which name do you want to save your file")
parser.add_argument("-o", "--output", type=str, help="Name of the file", default=None)
# Parse the arguments
args = parser.parse_args()
# Use the arguments in your code
print(args.url)
print(args.output, type(args.output))
download_file(args.url, args.output)