为什么此subprocess.check_call()不起作用? [英] Why doesn't this subprocess.check_call() work?
问题描述
我正在Ubuntu 12上使用Python 2.7运行这些代码片段.
I'm running these snippets with Python 2.7 on Ubuntu 12.
import subprocess
args = ['rsync', '--rsh="ssh"', '/tmp/a/', '127.0.0.1:/tmp/b/']
subprocess.check_call(args)
失败
import subprocess
args = ['rsync', '--rsh="ssh"', '/tmp/a/', '127.0.0.1:/tmp/b/']
call_string = ' '.join(args)
subprocess.check_call(call_string)
作品
第一个代码段导致rsync收到无效的命令行.使用此选项的简短版本( -e
)没有任何区别.如果我删除-rsh
部分,它将正常工作.
The first code snippet results in rsync getting an invalid command line. Using the short version of this option (-e
) doesn't make any difference. If I remove the --rsh
part it works fine.
使用 shell = True
没有任何区别.消除"ssh"中的引号没有什么区别,我希望保留引号,以便可以将参数传递给ssh.
Using shell=True
doesn't make any difference. Eliminating the quotes from "ssh" doesn't make any difference and I'd like to keep the quotes so I can pass arguments to ssh.
出什么问题了?
推荐答案
删除 ssh
周围的引号;外壳程序通常会解析它们并传递参数而没有引用:
Remove the quotes around ssh
; the shell would normally have parsed those and passed on the argument without the quoting:
import subprocess
args = ['rsync', '--rsh=ssh', '/tmp/a/', '127.0.0.1:/tmp/b/']
subprocess.check_call(args)
这篇关于为什么此subprocess.check_call()不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!