1

Currently im facing following problem:

I have 3 download links in a list. Only the last file in the list is downloaded completely. The others have a file size of one kilobyte.

Code:

from requests import get

def download(url, filename):
    with open(filename, "wb") as file:
        response = get(url, stream=True)
        file.write(response.content)

for link in f:
    url = link
    split_url = url.split("/")
    filename = split_url[-1]
    filename = filename.replace("\n", "")
    download(url,filename)

The result looks like this:

Result

How do I make sure that all files are downloaded correctly? All links are direct download links.

Thanks in advance!

EDIT:

I discovered it only happens when I read the links from the .txt

If I create the list in python like this:

links = ["http://ipv4.download.thinkbroadband.com/20MB.zip",
            "http://ipv4.download.thinkbroadband.com/10MB.zip",
            "http://ipv4.download.thinkbroadband.com/5MB.zip"]

... the problem doesnt appear.

reproduceable example:

from requests import get

def download(url, filename):
    with open(filename, "wb") as file:
        response = get(url, stream = True)
        file.write(response.content)

f = open('links.txt','r')
for link in f:
    url = link
    split_url = url.split("/")
    filename = split_url[-1]
    filename = filename.replace("\n", "")
    download(url,filename)

content of links.txt:

http://ipv4.download.thinkbroadband.com/20MB.zip
http://ipv4.download.thinkbroadband.com/10MB.zip
http://ipv4.download.thinkbroadband.com/5MB.zip
  • In `download` when you print/inspect the content and the filename are they correct? – wwii Feb 23 '20 at 17:28
  • Is there a reason you didn't try [this example in the docs](https://2.python-requests.org/en/master/user/quickstart/#raw-response-content) – wwii Feb 23 '20 at 17:33
  • Does this answer your question? [How to download image using requests](https://stackoverflow.com/questions/13137817/how-to-download-image-using-requests) – wwii Feb 23 '20 at 17:37
  • 1: Filenames are correct for all 3 files. Only the content of file 3 is correct. The other files seem to be empty 2,3: I tried those, same issue :( When I switch the links in the list I still have the same problem - only the last file is downloaded correctly – black_devil Feb 23 '20 at 18:18
  • Please provide a [mcve]. – AMC Feb 23 '20 at 18:51

1 Answers1

0

url = url.replace("\n", "")

solved it!