在我的本地机器上,我运行一个包含这一行的python脚本
bashCommand = "cwm --rdf test.rdf --ntriples > test.nt"
os.system(bashCommand)
Run Code Online (Sandbox Code Playgroud)
这很好用.
然后我在服务器上运行相同的代码,我收到以下错误消息
'import site' failed; use -v for traceback
Traceback (most recent call last):
File "/usr/bin/cwm", line 48, in <module>
from swap import diag
ImportError: No module named swap
Run Code Online (Sandbox Code Playgroud)
所以我所做的就是插入一个"print bashCommand",它在使用os.system()运行它之前打印我,而不是终端中的命令.
当然,我再次得到错误(由os.system(bashCommand)引起)但在该错误之前它在终端中打印命令.然后我只是复制了那个输出并在终端上做了一个复制粘贴,然后点击回车就可以了......
有没有人知道发生了什么?
我用subprocess模块调用不同的进程.但是,我有一个问题.
在以下代码中:
callProcess = subprocess.Popen(['ls', '-l'], shell=True)
Run Code Online (Sandbox Code Playgroud)
和
callProcess = subprocess.Popen(['ls', '-l']) # without shell
Run Code Online (Sandbox Code Playgroud)
两者都有效.阅读文档后,我开始知道这shell=True意味着通过shell执行代码.这意味着在缺席的情况下,该过程将直接启动.
那么我应该更喜欢我的情况 - 我需要运行一个进程并获得其输出.从shell内部或外部调用它有什么好处.
我可以在Linux的命令行中正常运行:
$ tar c my_dir | md5sum
Run Code Online (Sandbox Code Playgroud)
但是当我尝试用Python调用它时出现错误:
>>> subprocess.Popen(['tar','-c','my_dir','|','md5sum'],shell=True)
<subprocess.Popen object at 0x26c0550>
>>> tar: You must specify one of the `-Acdtrux' or `--test-label' options
Try `tar --help' or `tar --usage' for more information.
Run Code Online (Sandbox Code Playgroud) cmd= ["sudo", "cat", "{filepath}".format(filepath=filepath), "|","egrep", "-v","\'{filteruser}\'".format(filteruser=filteruser)]
Run Code Online (Sandbox Code Playgroud)
fileformat和filteruser也可以是空白的
[plugin_haproxy]
user= tara
host=localhost
filepath=/etc/haproxy/haproxy.global.inc
filter=
Run Code Online (Sandbox Code Playgroud)
这是我想在子进程上运行的命令,用pdb检查上面的变量值显示波纹管值并且看起来很棒
['sudo', 'cat', '/etc/haproxy/haproxy.global.inc', '|', 'egrep', '-v', "''"]
Run Code Online (Sandbox Code Playgroud)
Manullay运行代码sudo cat /etc/haproxy/haproxy.global.inc | 终端上的egrep -v"''"很棒
为什么子进程无法处理它.
"cat:|:没有这样的文件或目录\n \ncat:egrep:没有这样的文件或目录\n \ncat: