Python面料报价问题

时间:2013-07-25 14:21:45

标签: python quotes fabric

我花了差不多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

2 个答案:

答案 0 :(得分: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)")

答案 1 :(得分:1)

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

"[\"/..."

或者,对字符串使用单引号,这样就不需要转义:

'["/...'