嗨我有一個名爲image.txt的文件,其中包含大約5,00,000圖像url.I想讀取url並下載圖像並將其保存在一個目錄中。如果圖像不可下載I想要打印異常並重新開始下載其他文件,我可以通過優化的方式來實現。python中的圖像下載
import sys
import os
import urllib
def isValidFile(path):
if not os.path.isfile(path):
print "Path " + path + " doesn't exist! Aborting..."
exit(1)
def isValidDir(path):
if not os.path.isdir(path):
print "Path " + path + " doesn't exist! Aborting..."
exit(1)
def normalize(url):
url = url.split("/")[-1]
return url.split("\n")[0]
# Execution Starts Here
urls = sys.argv[1]
isValidFile(urls)
out_dir = sys.argv[2]
isValidDir(out_dir)
with open(urls) as url_array:
for url in url_array:
urllib.urlretrieve(url, os.path.join(out_dir, normalize(url)))
print("Images Downloaded")
什麼是與你的現有代碼的問題?你有錯誤嗎? –
我非常困惑你的代碼,以及你正在嘗試做什麼。你可以放置它並正確縮進(縮進不正確,尤其是在後半部分) – Zizouz212