Avoiding shell metacharacters in os.popen

David Fraser davidf at sjsoft.com
Fri Oct 1 06:02:09 EDT 2004


Nick Craig-Wood wrote:
> David Fraser <davidf at sjsoft.com> wrote:
> 
>> Nick Craig-Wood wrote:
>>
>>>3) is there an equivalent to the perl quotemeta() command.  This
>>>quotes meta-characters in a string for use in the shell.  Here is a
>>>suitable definition for Unix, but I don't think that \ quoting works
>>>in Windows
>>>
>>>   cmd = re.sub(r"(\W)", r"\\\1", cmd)
>>
>> I can't exactly remember the details but I now I've had painful 
>> experiences on Windows trying to do this kind of thing. It seems to mess 
>> up quoting parameters / exe file names
> 
> 
> Last time I did this for Windows was in C (urk!).  I put every
> parameter in double quotes and then doubled up any " in the parameter.
> This worked mostly but I don't think the OS parsed some bizarre cases
> properly.
> 
If I recall correctly (yikes) the problem was that Python's os.system 
would handle either a program name with spaces in it or a parameter with 
spaces in it, but quoting them both didn't work. Anyway if someone can 
get it to work nicely please post the details here :-)

David



More information about the Python-list mailing list