Avoiding shell metacharacters in os.popen

Nick Craig-Wood nick at craig-wood.com
Fri Oct 1 05:30:37 EDT 2004


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.

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list