Is there anything in the Python standard library that will properly parse/unparse strings for using in shell commands? I'm looking for the python analog to perl's String::ShellQuote::shell_quote
:
$ print String::ShellQuote::shell_quote("hello", "stack", "overflow's", "quite", "cool") hello stack 'overflow'\''s' quite cool
And, even more importantly, something which will work in the reverse direction (take a string and decompose it into a list).
Looks like
try: # py3 from shlex import quote except ImportError: # py2 from pipes import quote quote("hello stack overflow's quite cool") >>> '"hello stack overflow\'s quite cool"'
gets me far enough.
pipes.quote
is now shlex.quote
in python 3. It is easy enough to use that piece of code.
https://github.com/python/cpython/blob/master/Lib/shlex.py#L281
That version handles zero-length argument correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With