简体   繁体   中英

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM