如何在 os.system 命令中添加空格

how do I add spaces to os.system commands

我正在尝试在 os.system 命令的每一行的开头添加一些空格

example

os.system('pip install pyinstaller')

然后我明白了

Requirement already satisfied: pyinstaller in c:\users\pc\appdata\local\programs\python\python39\lib\site-packages (5.0.1)
Requirement already satisfied: pyinstaller-hooks-contrib>=2020.6 in c:\users\pc\appdata\local\programs\python\python39\lib\site-packages (from pyinstaller) (2021.5)
Requirement already satisfied: setuptools in c:\users\pc\appdata\local\programs\python\python39\lib\site-packages (from pyinstaller) (49.2.1)
Requirement already satisfied: altgraph in c:\users\pc\appdata\local\programs\python\python39\lib\site-packages (from pyinstaller) (0.17.2)
Requirement already satisfied: pefile>=2017.8.1; sys_platform == "win32" in c:\users\pc\appdata\local\programs\python\python39\lib\site-packages (from pyinstaller) (2021.9.3)
Requirement already satisfied: pywin32-ctypes>=0.2.0; sys_platform == "win32" in c:\users\pc\appdata\local\programs\python\python39\lib\site-packages (from pyinstaller) (0.2.0)
Requirement already satisfied: future in c:\users\pc\appdata\local\programs\python\python39\lib\site-packages (from pefile>=2017.8.1; sys_platform == "win32"->pyinstaller) (0.18.2)
WARNING: You are using pip version 20.2.3; however, version 22.1.2 is available.
You should consider upgrading via the 'c:\users\pc\appdata\local\programs\python\python39\python.exe -m pip install --upgrade pip' command.

但我想在每行中添加一些空格以显示视觉效果,以便与其余部分相匹配 我已经查了很多次了,我已经搜索了几个小时,但我找不到它,请帮忙

您可以在命令中将输出通过管道传输到 sed

os.system("pip install pyinstaller | sed 's/^/  /'")

尽管如评论所述,更好的解决方案是使用 subprocess.Popen()。然后你可以读取命令的输出,以任何你想要的方式对其进行转换,然后将修改后的输出写入终端。