1

I'm trying to download files from a site using the wget module. The code is really simple:

image = 'linkoftheimage'

wget.download(image)

This works fine, but it saves the file in the folder with the python script. My goal is to download it in a different folder, but I can't find a way to specify it. I tried a different approach with os module .

os.system(f'wget -O {directory} {image}')

This metod gives me an error: sh: -c: line 0: syntax error near unexpected token `('

So I tried another method:

with open(f'{directory}/photo %s.jpg' %a,'wb') as handler:
    handler.write(image)

This also didn't worked out.

Does anyone have an idea on how could I solve this?

Edoardo Balducci
  • 332
  • 1
  • 10
  • there are several libraries called `wget` which one are you using? – Nullman May 29 '22 at 10:10
  • See https://stackoverflow.com/questions/21867437/download-a-file-to-a-particular-folder-python – azro May 29 '22 at 10:11
  • https://pypi.org/project/wget/ – Edoardo Balducci May 29 '22 at 10:11
  • 2
    Also I'd suggest to use `requests` or `urllib` modules instead – azro May 29 '22 at 10:11
  • 1
    Did you read the page you show us ? `download(url, out, bar) contains out parameter` use `out` parameter – azro May 29 '22 at 10:12
  • Your `os.system` call should work; the error suggests that one of the variables contains a character which needs to be quoted. Like the `os.system` documentatron already tells you, a better solution is to use e.g. `subprocess.check_call(['wget', '-O', directory, image])` which avoids the shell (and thus the need for quoting, as well as [a slew of other possible issues](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess)) and gives you more control over several aspects of the behavior of the subprocess. (Better still to avoid a subprocess, too, though.) – tripleee May 29 '22 at 10:18
  • 1
    For what it 's worth, the location of the Python script is unimportant here. See [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory/66860904) – tripleee May 29 '22 at 10:20

1 Answers1

1

the package you specified has not been updated since 2015, it's repository is gone and so should probably be avoided. you can download files using the built-in requests module like so:

import requests

image_url = 'https://www.fillmurray.com/200/300'
file_destination = 'desired/destination/file.jpg'
res = requests.get(image_url)
if res.status_code == 200:  # http 200 means success
    with open(file_destination, 'wb') as file_handle:  # wb means Write Binary
        file_handle.write(res.content)
Nullman
  • 4,179
  • 2
  • 14
  • 30
  • I tried with this method but it gives me an error: `FileNotFoundError: [Errno 2] No such file or directory: '/Users/edoardo/Documents/media/photos/photo 1.jpg' ` I imagined that it was because the file i was trying to download was empty, but with the `wget` module the file downloaded, so I don't get what's wrong – Edoardo Balducci May 29 '22 at 10:45
  • @EdoardoBalducci does this path `/Users/edoardo/Documents/media/photos` exist? `open` will not create the path if it doesn't exist, only the file – Nullman May 29 '22 at 11:33
  • @EdoardoBalducci if you are on windows then that path is not good, absolute paths should start with a drive letter so try 'c:/Users/edoardo/Documents/media/photos/photo 1.jpg' – Nullman May 29 '22 at 11:42