使用 subprocess.check_call() 的错误 运行 shell 命令

Error running shell command with subprocess.check_call()

# -*- coding: utf-8 -*-
import rpyc
conn = rpyc.classic.connect(r"xx.xx.xx.xx")
s = conn.modules.subprocess.check_output(['file',u'`which dd`'])
print s

输出为:

`which dd`: cannot open ``which dd`' (No such file or directory)  

Process finished with exit code 0

当我在命令提示符下手动执行时,它会给我正确的输出:

/bin/dd: symbolic link to /bin/dd.coreutils

我的代码中是否存在任何 Unicode 错误

它 运行 在命令提示符下。但是如果你通过 subprocess 调用它(conn.modules.subprocess 也是如此),它会给你错误:

>>> subprocess.check_call(['file', '`which python`'])
`which python`: cannot open ``which python`' (No such file or directory)

因为在shell中,这将被执行为:

 mquadri$ file '`which python`'
`which python`: cannot open ``which python`' (No such file or directory)

但是你想运行它为:

mquadri$ file `which python`
/usr/bin/python: Mach-O universal binary with 2 architectures
/usr/bin/python (for architecture i386):    Mach-O executable i386
/usr/bin/python (for architecture x86_64):  Mach-O 64-bit executable x86_64

为了使上述命令 运行,将其作为字符串传递给 check_call,其中 shell=True 为:

>>> subprocess.check_call('file `which python`', shell=True)
/usr/bin/python: Mach-O universal binary with 2 architectures
/usr/bin/python (for architecture i386):    Mach-O executable i386
/usr/bin/python (for architecture x86_64):  Mach-O 64-bit executable x86_64