-1

My required output is :

 'grep "key\":\"value" test_File.txt'

for example, what i tried is..

>>> cmd = 'grep "key\":\"value" test_File.txt'
>>> cmd
 'grep "key":"value" test_File.txt'

but it prints two slashes when i try to escape the actual slash.

>>> cmd = 'grep "key\\":\\"value" test_File.txt'
>>> cmd
 'grep "key\\":\\"value" test_File.txt'
 >>> 

all i need is ,how can i get the first line as output ?

chetan honnavile
  • 352
  • 4
  • 17
  • 2
    just `print(cmd)`. You are printing the `repr` of the string. – juanpa.arrivillaga Mar 16 '17 at 06:02
  • What are you actually trying to accomplish here? This looks very much like you are trying to print a command to be executed by the shell, and being very confused about the shell's quoting rules. A valid `grep` command would look like `grep '"key":"value"' test_File.txt` but I am second-guessing several details here. With `subprocess` you want to avoid `shell=True` and pass just `['grep', '"key":"value"', 'test_File.txt']` instead of a string. – tripleee Mar 16 '17 at 06:15
  • yeah,i am trying to pass this as a command to subprocess to get executed , – chetan honnavile Mar 16 '17 at 06:25

1 Answers1

1

Don't get the escaped string confused with what's actually printed.

>>> cmd = '\'grep "key\\":\\"value\" test_File.txt\''
>>> print cmd
'grep "key\":\"value" test_File.txt'

I believe that matches your required output.

selbie
  • 91,215
  • 14
  • 97
  • 163