2

我花了将近 30 多分钟的时间来尝试所有可能的不同。终于现在我筋疲力尽了。有人可以帮我解决这个报价问题吗

def remote_shell_func_execute():
    with settings(host_string='user@XXX.yyy.com',warn_only=True):
            process = run("subprocess.Popen(\["/root/test/shell_script_for_test.sh func2"\],shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)")
            process.wait()
            for line in process.stdout.readlines():
                    print(line)

当运行工厂时,我得到

fab remote_shell_func_execute
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/Fabric-1.6.1-py2.7.egg/fabric/main.py",line 654, in main
docstring, callables, default = load_fabfile(fabfile)
File "/usr/local/lib/python2.7/site-packages/Fabric-1.6.1-py2.7.egg/fabric/main.py",line 165, in load_fabfile
 imported = importer(os.path.splitext(fabfile)[0])
 File "/home/fabfile.py", line 18
process = run("subprocess.Popen(\["/root/test/shell_script_for_test.sh        func2"\],shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)")
                                                                           ^
SyntaxError: invalid syntax
4

2 回答 2

3

只需使用单引号字符串。

run('subprocess.Popen(\["/root/test/shell_script_for_test.sh func2"\],shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)')

还是逃避内心"

run("subprocess.Popen(\[\"/root/test/shell_script_for_test.sh func2\"\],shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)")
于 2013-07-25T14:23:28.020 回答
1

转义引号时,转义反斜杠必须直接放在引号字符之前:

"[\"/..."

或者,对字符串使用单引号,这完全避免了转义的需要:

'["/...'
于 2013-07-25T14:22:58.737 回答