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.
source share