On 04Jan2011 12:21, Chris Weisiger <[email protected]> wrote:
| I want to sanitize some strings (e.g. escape apostrophes, spaces, etc.)
| before passing them to the commandline via subprocess. Unfortunately I can't
| seem to find any built-in function to do this. Am I really going to have to
| write up my own sanitizer? Not that it'd be much effort, but I'd much rather
| use an official function than risk forgetting something.

If it is for Bourne shell syntax, it's almost too simple to put in a
library: put into single quotes and replace all inner single quotes
with:

  '\''

You can play games with strings that are safe to not quote, etc but the
above is very simple and reliable. Something like (untested):

  "'"+s.replace("'", "'\\''")+"'"

Cheers,
-- 
Cameron Simpson <[email protected]> DoD#743
http://www.cskk.ezoshosting.com/cs/

You Know You're in the SCA When...
        ...the tunes you unconsciously hum are in Latin.
                - Cailfind ingen Grainne
_______________________________________________
Pythonmac-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG

Reply via email to