subprocess.run() 没有 return 任何输出

subprocess.run() doesn't return any output

我不明白为什么我的代码没有 return 任何输出。我是 运行 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)

打印给我空列表:['']

编辑:

  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)