Home:ALL Converter>Downloading files using Python requests

Downloading files using Python requests

Ask Time:2018-06-12T12:15:49         Author:CodySig

Json Formatter

I am writing a script to download files from Slack using the slack api and the requests library in Python. Anytime I download a file they all come out the same size (80kb) and they are all corrupted.

Here is my code:

def download_file(url, out):
    try:
        os.stat(out)
    except:
        os.mkdir(out)

    local_filename = out + '\\' + url.split('/')[-1]
    print('outputting to file: %s' % local_filename)

    response = requests.get(url, stream=True)
    with open(local_filename, 'wb') as f:
        response.raw.decode_content = True
        shutil.copyfileobj(response.raw,f)
    return local_filename

I have tried various different methods posted throughout SO to download the files but have not been successful. I have also checked the URL's I am getting from the Slack API and they are correct since I can paste them in my browser and download the file.

Any help would be appreciated!

Author:CodySig,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/50809167/downloading-files-using-python-requests
yy