Python fabric.api backslash

I am new to python and api. I am trying to use sudo functionality to run a sed command in a bash terminal that inserts some text after a specific line of text is found. Some of the text that I am trying to insert into the file I am editing contains backslashes that are apparently ignored by tags or cause syntax errors. I tried the options "shell = true" and "shell = false", but still no luck. How can I avoid the backslash? It seems that β€œshell = true” only eludes $ and β€œ. My code is below.

sudo ('sed -i "/ sometext / textwith \ backslash" /home/me/somefile.txt'shell=True)

+4
source share
2 answers

Try prefixing your string with r , which means that it will be interpreted as a raw string:

 sudo (r' sed -i "/sometext/a textwith\backslash" /home/me/somefile.txt',shell=True) 

See here for more information on string literals and their use in Python.

+5
source

OK, finally, it succeeded. RocketDonkey was right. You need a prefix with "r", but you also need to set "shell = False". This allowed it to ever work directly in the bash terminal to work when called from fabric.api.

Thanks RocketDonkey !!

+4
source

Source: https://habr.com/ru/post/1445418/


All Articles