Popen and wget, problems

Jesse hate at spam.com
Sun May 13 04:11:07 EDT 2007


Thx Rob!

Your solution works perfect!


"Rob Wolfe" <rw at smsnet.pl> wrote in message 
news:87d515ejil.fsf at merkury.smsnet.pl...
> "Jesse" <hate at spam.com> writes:
>
>> Hi all, I have a problem using wget and Popen. I hope someone can help.
>>
>>
>> -- Problem --
>> I want to use the command:
>> wget -nv -O "dir/cpan.txt" "http://search.cpan.org"
>> and capture all it's stdout+stderr.
>> (Note that option -O requires 'dir' to be existing before wget is 
>> executed)
>>
>> Popen doesn't work, while os.system and shell do. Popen will give the 
>> error:
>> dir/cpan.txt: No such file or directory
>>
>> While os.system and shell will give the correct result:
>> 06:52:40 URL:http://search.cpan.org/ [3657/3657] -> "dir1/cpan.txt" [1]
>
> [...]
>
>> -- Python Code using Popen with cmd arg list --
>> # imports
>> import os
>> from subprocess import Popen, PIPE
>>
>> # vars and create dir
>> cmd_set = ['wget', '-nv', '-O dir/cpan.txt', 'http://search.span.org']
>> cmd = ' '.join(cmd_set)
>> print "cmd: " + cmd
>> try:
>> os.makedirs('dir')
>> except:
>> print 'dir already exists'
>>
>>
>> # execute using Popen (does NOT work)
>> proc = Popen(cmd_set, stdout=PIPE, stderr=PIPE)
>> return_code = proc.wait()
>> if return_code == 0:
>> print "Success:\n%s" % (proc.stdout.read() + proc.stderr.read())
>> else:
>> print "Failure %s:\n%s" % (return_code, proc.stderr.read() +
>> proc.stdout.read())
>>
>>
>> # execute using os.system (does work)
>> os.system(cmd)
>>
>>
>> -- Python code output of Popen --
>> Failure 1:
>> dir/cpan.txt: No such file or directory
>>
>>
>> -- Question --
>> Why is Popen unable to correctly execute the wget, while os.system can?
>
> I don't know exactly why in this case Popen doesn't work,
> but the counterpart of os.system is Popen with option shell=True
> and the first parameter should be a string instead of list.
> That seems to work:
> proc = Popen("wget -nv -O dir/cpan.txt http://search.span.org",
>              shell=True, stdout=PIPE, stderr=PIPE)
>
> and this variant seems to work too:
> cmd_set = ['wget', '-nv', '-O', 'dir/cpan.txt', 'http://search.span.org']
>
> -- 
> HTH,
> Rob 




More information about the Python-list mailing list