views:

160

answers:

2

Hi! I'm trying to use wget with subprocess.

my attempts worked until I tried to download the page to a specified directory with this code:

url = 'google.com'
location = '/home/patrick/downloads'
args = ['wget', 'r', 'l 1' 'p' 'P %s' % location, url]

output = Popen(args, stdout=PIPE)

if I run this code in /home/patrick I get index.html in /home/patrick and not in /home/patrick/downloads.

Can you help me?

Thanks ;)

+2  A: 

You need to have hyphens and location should be just another argument:

args = ['wget', '-r', '-l', '1', '-p', '-P', location, url]
SilentGhost
And some spaces? All of those single-quoted strings are goign to get mashed together without spaces between them.
Adam Crossland
@adam: I don't think so
SilentGhost
Now that there are commas between them, I think that it would work. In the code that Patrick posted, the ones without commas, to make them separate elements of the array, all gget concatenated together. You are doing it correctly in your answer, SilentGhost.
Adam Crossland
Yeah! Thanks! It works :D I just forgot some commas after 'l 1' and 'p' ;)
patrick
A: 

Edit: popen from os intends to replace os.popen module. Hence, using os.popen is not recommended

Initially I thought it was popen from os.

If you are using popen from os

#wget 'http://google.com/' -r -l 1 -p -P /Users/abhinay/Downloads

from os import popen

url = 'google.com'
location = '/Users/abhinay/Downloads'
args = ['wget %s', '-r', '-l 1', '-p', '-P %s' % location, url]

output = popen(' '.join(args))

and using Popen from subprocess

#wget 'http://google.com/' -r -l 1 -p -P Downloads/google

from subprocess import Popen

url = 'google.com'
location = '/Users/abhinay/Downloads'
#as suggested by @SilentGhost the `location` and `url` should be separate argument
args = ['wget', '-r', '-l', '1', '-p', '-P', location, url]

output = Popen(args, stdout=PIPE)

Let me know if I'm missing something.

Thx!

abhiomkar
docs say: *args* should be a string, or a sequence of program arguments.
SilentGhost
subprocess.Popen should be used instead of os.popen
Corey Goldberg
@SilentGhost Initially I thought it was popen from os, I edited my answer now.@Corey Thanks for the suggestion! looks like `popen` is an older and replaced by `Popen`.
abhiomkar
gosh, I I couldn't even imagine that you'd be advising `os.open`, it indeed required a string as a first argument.
SilentGhost
I'm learning lot of things here! Love Stackoverflow! :)
abhiomkar