Reputation: 13
I am trying to loop through a text file with different website links to download the images. I also want them to have a unique file name. Just a loop counter as you see so the three images would be 1.jpg
, 2.jpg
, 3.jpg
. Yet I am only getting the last image and the file is named 0.jpg
. I have tried a couple of different methods but this seemed the best but still no luck. Any suggestions on next steps?
import urllib
input_file = open('Urls1.txt','r')
x=0
for line in input_file:
URL= line
urllib.urlretrieve(URL, str(x) + ".jpg")
x+=1
Upvotes: 1
Views: 1023
Reputation: 8215
rewrite the code by indenting the last two lines thus
import urllib
input_file = open('Urls1.txt','r')
x=0
for line in input_file:
URL= line
urllib.urlretrieve(URL, str(x) + ".jpg")
x+=1
Indentation is significant in Python. Without it, the last two statements are only executed after the loop has completed. Thus you only retrieve the last URL in the file.
Upvotes: 1