Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading