forked from csev/py4e
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcurl3.py
More file actions
28 lines (23 loc) · 651 Bytes
/
curl3.py
File metadata and controls
28 lines (23 loc) · 651 Bytes
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
import os
import urllib.request, urllib.parse, urllib.error
print('Please enter a URL like http://data.pr4e.org/cover.jpg')
urlstr = input().strip()
img = urllib.request.urlopen(urlstr)
# Get the last "word"
words = urlstr.split('/')
fname = words[-1]
# Don't overwrite the file
if os.path.exists(fname):
if input('Replace ' + fname + ' (Y/n)?') != 'Y':
print('Data not copied')
exit()
print('Replacing', fname)
fhand = open(fname, 'wb')
size = 0
while True:
info = img.read(100000)
if len(info) < 1: break
size = size + len(info)
fhand.write(info)
print(size, 'characters copied to', fname)
fhand.close()