subprocess.run() 不返回任何输出



我不明白为什么我的代码不返回任何输出。我正在运行python 3.8

bash_command = ['compgen', '-c']
process = subprocess.run(bash_command,
check=True,
text=True,
shell=True,
executable='/bin/bash',
capture_output=True
)
software_list = process.stdout.split('n')
print(software_list)

Print给出空列表:["]

编辑:

  1. compgen是bash命令,它列出了PATH
  2. 中所有可用的命令,包括内置和已安装的程序
  3. 我的机器上已经安装了compgen

当您运行指定shell=True的程序时,命令参数可以是单个字符串而不是字符串列表。这是一个需要使用单个字符串的实例:

import subprocess

bash_command = 'compgen -c' # single string
process = subprocess.run(bash_command,
check=True,
text=True,
shell=True,
executable='/bin/bash',
capture_output=True
)
software_list = process.stdout.split('n')
print(software_list)

最新更新