"No such file or directory" 在 python 中调用 fc-list 时出错

"No such file or directory" error when calling fc-list in python

我正在尝试抓取当前托管服务器上安装的字体列表的终端 window。我写了下面的代码:

 import subprocess
 cmd = 'fc-list'
 output = subprocess.Popen(cmd, stdout=subprocess.PIPE ).communicate()[0]

但是当我调用这段代码时,引发了一个异常:

[Errno 2] No such file or directory

我可以打开一个终端 window,这很好用。我做错了什么?

您需要提供可执行文件的绝对路径。当你打开一个终端 window 时,你就会有一个 shell 运行ning 它将在 $PATH 中搜索以找到该程序。当您通过子进程直接 运行 程序时,您没有 shell 来搜索 $PATH。 (注意:可以告诉子进程您确实想要 shell,但这通常会导致安全漏洞)

这是您想要使用的内容:

 import subprocess
 cmd = '/usr/local/bin/fc-list'
 output = subprocess.Popen(cmd, stdout=subprocess.PIPE ).communicate()[0]