def quote_for_shell(s):
    returned = s.replace("\\", "\\\\").replace('"', '\\"').replace("$", "\\$")
    if " " in returned:
        returned = '"%s"' % returned
    return returned
>>> print quote_for_shell('this is a " string')
dataset/ETHPy150Open CouchPotato/CouchPotatoServer/libs/git/utils.py/quote_for_shell