-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadSingle.py
More file actions
34 lines (27 loc) · 1.13 KB
/
DownloadSingle.py
File metadata and controls
34 lines (27 loc) · 1.13 KB
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
import os
import urllib.request
import time
destination_folder = "flamingos_files"
# open the file with the images URLs, read them and close the file
my_file = open("source_files.txt", "r")
urls = my_file.readlines()
my_file.close()
# create and change working directory
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
os.chdir(destination_folder)
# initiate download of all described images in separate threads
for index, image_url in enumerate(urls):
print("Image %s: %s - starting" % (index + 1, time.ctime(time.time())))
handle_remote = urllib.request.urlopen(image_url.strip())
# The os module is used to extract the name of the downloading file,
# so it's used to create a file with the same name on our machine.
file_name = os.path.basename(image_url.strip())
# download the file a kilobyte at a time and write it to disk
with open(file_name, "wb") as file_handler:
while True:
part = handle_remote.read(1024)
if not part:
break
file_handler.write(part)
print("Image %s: %s - ending" % (index + 1, time.ctime(time.time())))