Home:ALL Converter>Downloading files in python not working properly

Downloading files in python not working properly

Ask Time:2022-01-28T13:34:39         Author:coder684

Json Formatter

I have a script to download the files from a file full of links(one link per line). To do the same, I made a for loop which would download every link in each line, but unexpectedly it's only downloading the last link in the file. Here is my code for the python script that I tried:

import time
import requests
import requests
import os.path

with open('Links.txt', 'r', encoding='utf-8') as link_file:
    for link in link_file:
                def download_url(_url):
                    print("downloading: ", _url)
                    file_name_start_pos = _url.rfind("/") + 1
                    file_name = _url[file_name_start_pos:]
                    r = requests.get(_url, stream=True)
                    if r.status_code == requests.codes.ok:
                        with open(file_name, 'wb') as f:
                            for data in r:
                                f.write(data)
                    return os.path.exists(file_name)
                download_url(link)

Please help with my script, any alternatives would be fine as well.

Author:coder684,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/70889224/downloading-files-in-python-not-working-properly
yy