Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python fabric.api backslash hell

I'm new to python and fabric api. I'm trying to use the sudo functionality to run a sed command in bash terminal which insert some text after a particular line of text is found. Some of the text I'm trying to insert into the file I'm modifying contains backslashes which seem to either be ignored by fabric or cause syntax errors. I've tried "shell=true" and "shell=false" options but still no luck. How can I escape the backslash? It seems "shell=true" only escapes $ and ". My Code below.

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

like image 463
Hoani888 Avatar asked Nov 12 '12 03:11

Hoani888


1 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 additional information on string literals and their usage in Python.

like image 127
RocketDonkey Avatar answered Sep 19 '22 07:09

RocketDonkey