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



我试图在os的每行开始添加一些空格。系统命令

example
os.system('pip install pyinstaller')

然后得到这个

Requirement already satisfied: pyinstaller in c:userspcappdatalocalprogramspythonpython39libsite-packages (5.0.1)
Requirement already satisfied: pyinstaller-hooks-contrib>=2020.6 in c:userspcappdatalocalprogramspythonpython39libsite-packages (from pyinstaller) (2021.5)
Requirement already satisfied: setuptools in c:userspcappdatalocalprogramspythonpython39libsite-packages (from pyinstaller) (49.2.1)
Requirement already satisfied: altgraph in c:userspcappdatalocalprogramspythonpython39libsite-packages (from pyinstaller) (0.17.2)
Requirement already satisfied: pefile>=2017.8.1; sys_platform == "win32" in c:userspcappdatalocalprogramspythonpython39libsite-packages (from pyinstaller) (2021.9.3)
Requirement already satisfied: pywin32-ctypes>=0.2.0; sys_platform == "win32" in c:userspcappdatalocalprogramspythonpython39libsite-packages (from pyinstaller) (0.2.0)
Requirement already satisfied: future in c:userspcappdatalocalprogramspythonpython39libsite-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:userspcappdatalocalprogramspythonpython39python.exe -m pip install --upgrade pip' command.

,但我想在每一行添加一些空格,以便视觉效果,使其适合其余部分我已经查了很多次,我已经搜索了几个小时,但我找不到它,请帮助

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

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

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

最新更新