Skip to content

Commit d09b800

Browse files
committed
python
1 parent 0613062 commit d09b800

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

day60(Command Line).py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# import argparse
2+
3+
# parser = argparse.ArgumentParser()
4+
5+
# parser.add_argument("url",
6+
# help="url of the file to download" )
7+
8+
# parser.add_argument("output",
9+
# help="by which name you want to save" )
10+
11+
# args = parser.parse_args()
12+
# print(args.url)
13+
# print(args.output)
14+
import argparse
15+
import requests
16+
17+
def download_file(url, local_filename):
18+
if local_filename is None:
19+
local_filename = url.split('/')[-1]
20+
# NOTE the stream=True parameter below
21+
with requests.get(url, stream=True) as r:
22+
r.raise_for_status()
23+
with open(local_filename, 'wb') as f:
24+
for chunk in r.iter_content(chunk_size=8192):
25+
# If you have chunk encoded response uncomment if
26+
# and set chunk_size parameter to None.
27+
#if chunk:
28+
f.write(chunk)
29+
return local_filename
30+
31+
parser = argparse.ArgumentParser()
32+
33+
# Add command line arguments
34+
parser.add_argument("url", help="Url of the file to download")
35+
# parser.add_argument("output", help="by which name do you want to save your file")
36+
parser.add_argument("-o", "--output", type=str, help="Name of the file", default=None)
37+
38+
# Parse the arguments
39+
args = parser.parse_args()
40+
41+
# Use the arguments in your code
42+
print(args.url)
43+
print(args.output, type(args.output))
44+
download_file(args.url, args.output)

0 commit comments

Comments
 (0)