Need help running external program

Steve Holden steve at holdenweb.com
Wed Mar 2 17:49:16 EST 2005


Rigga wrote:
> Tim Jarman wrote:
> 
> 
>>Rigga wrote:
>>
>>
>>>Brian van den Broek wrote:
>>>
>>>
>>>>Rigga said unto the world upon 2005-02-27 15:04:
>>
>>(snip stuff about raw strings)
>>
>>
>>>Thanks for all your help with this it is appreciated, one further
>>>question though, how do I pass a variable to the external program while
>>>using the r"""
>>>
>>>Thanks
>>>
>>>RiGGa
>>
>>I'm not sure I understand the question. Say you have:
>>
>>parameter = r"my \funky \text"
>>
>>then surely you just pass it to your external program using whichever
>>method you like, e.g.
>>
>>import os
>>os.execl("your_external_prog", parameter) # replaces the current process
>>
>>or some variant of:
>>
>>return_code = os.spawnl(os.P_WAIT, "your_external_prog", parameter)
>>
>>or you can build a command line:
>>
>>command = "your_external_prog %s" % parameter
>>return_code = os.system(command)
>>
>>(see docs on the os module for more variations on this theme than you can
>>shack a stick at)
>>
>>It's just a string, after all.
>>
>>
>>
> 
> This is the command I am trying to run:
> 
> feed is a list of web addresses
> 
> output, input = popen2("wget -q %s -O - | tr '\r' '\n' | tr \' \" | sed -n
> 's/.*url="\([^"]*\)".*/\1/p'" % feed[counter])
> 
> But it does not work, if I escape the string using r""" and hard code in the
> web address rather than use %s and feed[counter] it works, my question is
> how do I escape the string to get it to work with the %s and feed[counter]
> 
> Im new to python as you can tell :-)

Right, using raw strings (r" ... ") makes sure that backslashes in the 
literal are retained rather than used as escapes. Socould you show us an 
example where the expression (using  ... % feed[counter]) gives you a 
different value from "hard coding the web address"?

I suspect if you use a raw string for the format then that will be 
enough - in other words, does

output, input = popen2(r"""wget -q %s -O - | tr '\r' '\n' | tr \' \" | 
sed -n 's/.*url="\([^"]*\)".*/\1/p'""" % feed[counter])

work?

regards
  Steve
-- 
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005                      http://www.pycon.org/
Steve Holden                           http://www.holdenweb.com/



More information about the Python-list mailing list