Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download to a specific directory?

Tags:

python

This is how I'm downloading:

    webbrowser.open(download_url)

So I've got the URL and I'm just opening it with the webbrowser module. This downloads whatever is on the other side of that URL to the default download location of my default browser.

How can I change this location to something I specify in my program? I feel like this should have a simple solution but I can't find anything about it on the web. I tried os.chdir() and that had no effect.

like image 527
kamatama Avatar asked Jan 09 '14 11:01

kamatama


People also ask

How do you wget a file to a specific directory?

When downloading a file, Wget stores it in the current directory by default. You can change that by using the -P option to specify the name of the directory where you want to save the file. Download the jQuery file you downloaded previously, but this time save it in the Downloads subdirectory.

How do I set PHP to download to a specific directory?

Create cURL session. Declare a variable and store the directory name where the downloaded file will save. Use the basename() function to return the file basename if the file path is provided as a parameter. Save the file to the given location.

How do I download a file from a specific location in Python?

One of the simplest way to download files in Python is via wget module, which doesn't require you to open the destination file. The download method of the wget module downloads files in just one line. The method accepts two parameters: the URL path of the file to download and local path where the file is to be stored.


1 Answers

Use urllib.urlretrieve (urllib.request.urlretrieve in Python 3.x):

import urllib

urllib.urlretrieve('http://example.com/file.ext', '/path/to/directory/filename.ext')

NOTE the second argument should be file path, not directory path.

like image 79
falsetru Avatar answered Oct 15 '22 10:10

falsetru