Leearn2303
Leearn2303

Reputation: 141

Download images from url that is stored in .txt file?

I'm using python 3.6 on Windows 10, I want to download images so that their urls are stored in 1.txt file.

This is my code:

import requests
import shutil

file_image_url = open("test.txt","r")
while True:
    image_url = file_image_url.readline()
    filename = image_url.split("/")[-1]
    r = requests.get(image_url, stream = True)
    r.raw.decode_content = True
    with open(filename,'wb') as f:
        shutil.copyfileobj(r.raw, f)

but when I run the code above it gives me this error:

Traceback (most recent call last):
File "download_pictures.py", line 10, in <module>
with open(filename,'wb') as f:
OSError: [Errno 22] Invalid argument: '03.jpg\n'

test.txt contains:

https://mysite/images/03.jpg
https://mysite/images/26.jpg
https://mysite/images/34.jpg

When I tried to put just one single URL on test.txt, it works and downloaded the picture, but I need to download several images.

Upvotes: 0

Views: 884

Answers (2)

user459872
user459872

Reputation: 24592

f.readline() reads a single line from the file; a newline character (\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline.

You are passing this filename(with \n) to open function(hence the OSError). So you need to call strip() on filename before passing into open.

Upvotes: 3

Stoobish
Stoobish

Reputation: 1362

Your filename has the new line character (\n) in it, remove that when you’re parsing for the filename and it should fix your issue. It’s working when you only have one file path in the txt file because there is only one line.

Upvotes: 0

Related Questions