popen eating quotes?

B

Bryan

i have a batch file that contains these two lines:

-- args.bat

echo %1
echo %2


when i run args.bat from the command line it works correctly... notice that
abc has quotes in the first example and not in the second.

C:\Temp>"c:\program files\test\args" -vv abc
C:\Temp>echo -vv
-vv
C:\Temp>echo abc
abc

C:\Temp>"c:\program files\test\args" -vv "abc"
C:\Temp>echo -vv
-vv
C:\Temp>echo "abc"
"abc"


from python, i'm having trouble getting the quotes around abc to work
.... i, o = os.popen4('"c:/program files/test/args.bat" -vv %s' % arg)
.... print o.read()
....C:\python\python23\lib\site-packages>echo -vv
-vv
C:\python\python23\lib\site-packages>echo abc
abc
'c:/program' is not recognized as an internal or external command,
operable program or batch file.


what is going on here? why does "abc" effect the uoted program name. does
anyone know how to fix this? how to pass a quoted arguments through to
popen? i need to do this because in my real code, i'm passing paths as
arguments and need them quoted.

i'm using python 2.3 on winxp.

thanks,

bryan
 
J

Jeff Epler

This has been discussed before. One such thread that I participated in
was
http://groups.google.com/groups?hl=...selm=0000161b%40bossar.com.pl&rnum=1&filter=0
[apologies for the long URL, I don't know how to get a good memorable
URL for a google groups search]

This really is a cesspool on Windows, and the fault isn't Python's.
Python is just calling the Windows functions, which insist on behaving
badly compared to the Unix behavior.

unix bigot'ly yours,
jeff
 
B

Bryan

Jeff Epler said:
This has been discussed before. One such thread that I participated in
was
http://groups.google.com/groups?hl=...selm=0000161b%40bossar.com.pl&rnum=1&filter=0
[apologies for the long URL, I don't know how to get a good memorable
URL for a google groups search]

This really is a cesspool on Windows, and the fault isn't Python's.
Python is just calling the Windows functions, which insist on behaving
badly compared to the Unix behavior.

unix bigot'ly yours,
jeff

thanks for the link, but it didn't help me solve my problem. i just read
many articles posted about popen and windows and there seems to be a
solution to this problem. i need to pass a path to a program using popen so
i can capture the output. since the path and/or file name can have spaces
in it, i need to quote it. what's the way to get around this issue? i saw
it mentioned in several places to try win32pipe.popen, but it had the same
problem as os.popen. so, is this something that cannot be done in python at
all with no work-around? is there another way besides using os.system or
os.popen?
.... i, o = os.popen4('"c:/program files/test/args.bat" -vv %s' % arg)
.... print o.read()
....'c:/program' is not recognized as an internal or external command,
operable program or batch file.

thanks,

bryan
 
S

Steven Taschuk

Quoth Jeff Epler:
http://groups.google.com/groups?hl=...selm=0000161b%40bossar.com.pl&rnum=1&filter=0
[apologies for the long URL, I don't know how to get a good memorable
URL for a google groups search]

Not memorable, but at least shorter, is
<http://groups.google.com/[email protected]>
In general all you need is the threadm or selm parameter, which
gives the message-id of the post in question; if memory serves,
threadm and selm differ in whether the resulting page shows the
rest of the thread.

(Ignore the selm in your URL above; it's inside the prev
parameter, which stores information about where you were before
arriving at the page the URL is actually for.)

It seems that Google also assigns an id to each thread; with
suitable poking around (see, e.g., the source for the page at the
above URL) you can locate a 'th' parameter which can be used in
place of the threadm/selm parameter if you wish to refer to the
thread as a whole. In your case that's
<http://groups.google.com/groups?th=77576209b6262476>
(As this illustrates, the resulting URL is often shorter than one
using the message-id.) The first ten messages in the thread
appear on the page obtained thus, with anchors, so you can refer
to them individually by appending '#link1', '#link2', etc. This
makes nice short URLs too (though I'm not certain that *which*
messages such anchors refer to is constant over time as the thread
grows).

All discovered empirically. Use at your own risk.
 
R

Rob Williscroft

Bryan wrote in
... i, o = os.popen4('"c:/program files/test/args.bat" -vv %s' % arg)
... print o.read()
...
'c:/program' is not recognized as an internal or external command,
operable program or batch file.

I just got the following working where os.getcwd() is returning:
'C:\\Documents and Settings\\Rob\\Desktop'

.... i, o = os.popen4( "\"" + os.getcwd() + "\\aaa.bat\"" )
.... print o.read()
.... i.close()
.... o.close()
....


If this dosen't help, then (sound of large hammer being removed
from a tool box) maybe this will:

def g(arg):
x = os.getcwd()
os.chdir( "c:/program files/test/" )
i, o = os.popen4( 'args.bat -vv %s' % arg )
os.chdir( x )
print o.read()


Rob.
 
B

Bryan

Rob Williscroft said:
Bryan wrote in

I just got the following working where os.getcwd() is returning:
'C:\\Documents and Settings\\Rob\\Desktop'


... i, o = os.popen4( "\"" + os.getcwd() + "\\aaa.bat\"" )
... print o.read()
... i.close()
... o.close()
...


If this dosen't help, then (sound of large hammer being removed
from a tool box) maybe this will:

def g(arg):
x = os.getcwd()
os.chdir( "c:/program files/test/" )
i, o = os.popen4( 'args.bat -vv %s' % arg )
os.chdir( x )
print o.read()


Rob.


rob,

thanks... this is exactly the solution i came up with too. it does remove
the path, but not the spaces in the file name. i _might_ be able to live
with this hack though.

the code i'm porting to python is from perl, and the perl code doesn't have
the popen issue.

@xxxoutput=`xxx -vv "$file"`;

now i won't be able show how cool python is :(

bryan
 
B

Ben Finney

Not memorable, but at least shorter, is
<http://groups.google.com/[email protected]>
In general all you need is the threadm or selm parameter, which
gives the message-id of the post in question; if memory serves,
threadm and selm differ in whether the resulting page shows the
rest of the thread.

Fortunately, the Message-Id: is available in the message itself (it's
part of every NNTP posting), so even without Google you theoretically
have complete information to give the Google Groups URL for any post:

<http://groups.google.com/groups?threadm=MESSAGE-ID>

for threaded, or

<http://groups.google.com/groups?selm=MESSAGE-ID>

for the message alone.
All discovered empirically. Use at your own risk.

Likewise.
 
C

Christopher Koppler

Quoth Jeff Epler:
http://groups.google.com/groups?hl=...selm=0000161b%40bossar.com.pl&rnum=1&filter=0
[apologies for the long URL, I don't know how to get a good memorable
URL for a google groups search]

Not memorable, but at least shorter, is
<http://groups.google.com/[email protected]>
In general all you need is the threadm or selm parameter, which
gives the message-id of the post in question; if memory serves,
threadm and selm differ in whether the resulting page shows the
rest of the thread.

(Ignore the selm in your URL above; it's inside the prev
parameter, which stores information about where you were before
arriving at the page the URL is actually for.)

It seems that Google also assigns an id to each thread; with
suitable poking around (see, e.g., the source for the page at the
above URL) you can locate a 'th' parameter which can be used in
place of the threadm/selm parameter if you wish to refer to the
thread as a whole. In your case that's
<http://groups.google.com/groups?th=77576209b6262476>
(As this illustrates, the resulting URL is often shorter than one
using the message-id.) The first ten messages in the thread
appear on the page obtained thus, with anchors, so you can refer
to them individually by appending '#link1', '#link2', etc. This
makes nice short URLs too (though I'm not certain that *which*
messages such anchors refer to is constant over time as the thread
grows).

All discovered empirically. Use at your own risk.

If all you want is a short URL for posting, have a look at
http://tinyurl.com/

Jeff's *long* URL would then be: http://tinyurl.com/iy1q

--Christopher
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top