-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday60(Command Line).py
44 lines (35 loc) · 1.27 KB
/
day60(Command Line).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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# import argparse
# parser = argparse.ArgumentParser()
# parser.add_argument("url",
# help="url of the file to download" )
# parser.add_argument("output",
# help="by which name you want to save" )
# args = parser.parse_args()
# print(args.url)
# print(args.output)
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)