1

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
link89
  • 1,064
  • 10
  • 13
  • 9
    `print(repr(s))`? – Abdul Niyas P M Jan 09 '23 at 07:12
  • Wow, I never think of it as I thought `repr` is just a method to show human friendly message in exception. – link89 Jan 09 '23 at 07:19
  • 1
    Does this answer your question? [How to print a string that contains quotes in python](https://stackoverflow.com/questions/56018995/how-to-print-a-string-that-contains-quotes-in-python) – hc_dev Jan 09 '23 at 07:23
  • Atually the command is build in python, the flow is: build a dynamic python script string in python => run the python string on remote node via `fabric` – link89 Jan 09 '23 at 07:26
  • @hc_dev No, it's for static string, I am looking for a method to handle dynamic string. I just update my use case in the original question. – link89 Jan 09 '23 at 07:34
  • Python2: https://stackoverflow.com/questions/20056548/how-to-print-double-quotes-around-a-variable Python3: https://stackoverflow.com/questions/48365860/python3-how-to-use-print-to-print-a-string-with-quote – Tzane Jan 09 '23 at 07:38

2 Answers2

2

Yes, there is --> print(repr(s))

Output:

'hello world'

You can read more about repr() for example here --> https://www.geeksforgeeks.org/python-repr-function/

FoxFil
  • 320
  • 10
2

You can use repr to produce a valid literal of your string. However, instead of producing a valid Python string literal inside a valid bash literal, you should probably pursue something like this instead:

from shlex import quote

command = f'''echo -n {quote(s)} | python -c 'from glob import glob; import sys; print(glob(sys.stdin.read()))'''

Keep your Python code static and supply arguments to it, in this case via stdin.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • That's anthoer option, thank you! – link89 Jan 09 '23 at 07:43
  • This is the better solution, when taking security into consideration. – Cow Jan 09 '23 at 07:47
  • But glob is just one of my use cases, there may be cases requiring multiple params in the future. I would expected more general solution. For example, use `shlex` and environment variable instead of `stdin`. – link89 Jan 09 '23 at 07:55
  • Yes, environment variables are another option, as is passing a JSON object via stdin or whatever. You'll have to figure out the right combination for your particular case. – deceze Jan 09 '23 at 07:57