Given the following python script:
s = "hello world"
print(s)
When you run it you will get
hello world
If I want the output to be
"hello world"
Is there any build-in quote/escape method can do this? For example
s = "hello world"
print(quote(s))
Here is my real world use case: I want to run glob
on a remote machine via fabric. And the search pattern of glob
is provided by user. So I need to ensure the string are quoted properly. Here is the sample code (I already know repr
is the right method)
import shlex
glob_pattern = 'some-data/*' # user input, maybe malform
script = 'from glob import glob; print(glob({}))'.format(repr(glob_pattern))
cmd = 'python -c {}'.format(shlex.quote(script))
connection.run(cmd) # use a fabric connection to run script on remote node