473,695 Members | 3,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Popen and wget, problems

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]

-- Background info about wget --
-Option -nv: -nv, --no-verbose turn off verboseness, without
being quiet.
-Option -O: -O, --output-document=FILE write documents to FILE.

Note that wget requires any directories in the file-path of option -O to be
existing before the wget command is executed.
-- 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('di r')
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.re ad() + proc.stderr.rea d())
else:
print "Failure %s:\n%s" % (return_code, proc.stderr.rea d() +
proc.stdout.rea d())
# 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?

May 12 '07 #1
3 7833
"Jesse" <ha**@spam.comw rites:
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('di r')
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.re ad() + proc.stderr.rea d())
else:
print "Failure %s:\n%s" % (return_code, proc.stderr.rea d() +
proc.stdout.rea d())
# 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
May 12 '07 #2
Thx Rob!

Your solution works perfect!
"Rob Wolfe" <rw@smsnet.plwr ote in message
news:87******** ****@merkury.sm snet.pl...
"Jesse" <ha**@spam.comw rites:
>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('d ir')
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.re ad() + proc.stderr.rea d())
else:
print "Failure %s:\n%s" % (return_code, proc.stderr.rea d() +
proc.stdout.re ad())
# execute using os.system (does work)
os.system(cm d)
-- 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
May 13 '07 #3
js
Hi Jesse.
cmd_set = ['wget', '-nv', '-O dir/cpan.txt', 'http://search.span.org ']
[snip]
>proc = Popen(cmd_set, stdout=PIPE, stderr=PIPE)
wget will treat this as
$ wget -nv '-O dir/cpan.txt' "http://search.cpan.org "

And will emit the following error because there's no pathname ' dir/cpan.txt'.
(Note the pathname has a trailing space.)

dir/cpan.txt: No such file or directory

Replace '-O dir/cpan.txt" with '-Odir/cpan.txt' or '-O', 'dir/cpan.txt'
and it should work.

Hope this helps
May 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8169
by: Martin | last post by:
I have a PHP script that should be run a few times a day. Currently I run this script manually as I haven't been able to set up Lynx or wget to run it as a cronjob. Or to be more specific: the script works, but both wget and Lynx fail after 10 or 20 redirects, then it warns: wget: 20 redirections exceeded. Lynx: Alert!: Redirection limit of 10 URL's reached. What the scripts does is download various HTML pages (allowed by the owners)...
1
3987
by: LRW | last post by:
In a PHP script I have the following two lines: $thumbproc = escapeshellcmd("wget -q -T 2000 http://(A URL)/pa-thumbs/createthumbs.php?dir=".$order_po." &"); exec("$thumbproc 2>&1", $output); Works perfectly...except, it always waits until it finished with the wget before it continues in the script. Normally this wouldn't be a problem, except this wget is calling a page that's doing some image manipulation that can take up to two
7
7719
by: Ken | last post by:
How do I call c:\wget\wget.exe from a php or html file? so I can use php variables to define the file names to download. I want to set up wget to download several files from a server to my hard drive so they can be used internally. Ken
2
3426
by: Tim Black | last post by:
In my recent experience, popen os pipes always fail when cwd is a UNC path. Can anyone shed any light on this? Although I've seen lots of UNC path-related problems in this newsgroup, I've not been able to find anything specifically about os pipes and UNC paths. Here's a session dump that demonstrates what I'm talking about: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license" for more...
6
1847
by: Robin Siebler | last post by:
I have a bunch of command line tests that I need to automate. I've run into too many bugs with the default Win2k command shell, so I need to either purchase 4NT or handle the logic and output processing with Python. I'm looking for experiences, comments, problems, etc. Also, I'm trying to figure out how to use popen(). To say that the documentation and expamples available for this is sparse would be the understatment of the century! ...
5
8655
by: Michele Simionato | last post by:
I was looking at Python 2.4 subprocess.Popen. Quite nice and handy, but I wonder why a "kill" method is missing. I am just adding it via subclassing, class Popen(subprocess.Popen): def kill(self, signal = SIGTERM): os.kill(self.pid, signal) but I would prefer to have it in the standard Popen class. I am surprised it is not there. Any comments?
1
3610
by: Remulac | last post by:
I am in the process of getting ASCII data down from a server that will print out the ASCII version of a proprietary format when you click "get ASCII" on the site. I'm trying to bring it down programattically so that I get an ASCII file without displaying it in a window. So far, I've used wget from a DOS batch file, but I'm having problems when the URL has a "%" anywhere in it. How do I escape this character? Beyond that, is there a...
12
10984
by: Eric_Dexter | last post by:
I am trying to modify a programming example and I am coming up with two problems... first is that I can't seem to pass along the arguments to the external command (I have been able to do that with the old module and cmd is the command I wish to try) all the output seems to be returned as one line (at least when I run the program in spe). import subprocess from os import system cmd = """gawk -f altertime.awk -v time_offset=4 -v
3
2909
by: Mathieu Prevot | last post by:
Hi it seems the script (A) finishes before the downloading ends, and the (B) version doesn't (wanted behavior) ... this is unexpected. What happens ? (A) ============================================ class vid(threading.Thread): def __init__(self):
0
8640
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9122
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9001
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8860
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7672
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3018
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1984
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.