首页 > 解决方案 > 使用 Python 保存时来自终端的数据会被破坏

问题描述

我正在尝试从 ubuntu 终端中的命令获取数据并在 python 中使用它。当我在 python 中打印输出时,我得到了正确的东西(部分数据):

[+] Boot and services
------------------------------------
- Service Manager [ systemd ]
- Checking UEFI boot [ DISABLED ]
- Checking presence GRUB2 [ FOUND ]
- Checking for password protection [ NONE ]
- Check running services (systemctl) [ DONE ]
Result: found 35 running services

但是当我保存它时,在变量\到文件\中将输出权发送到文本文件(在终端中使用 >)我得到了这个:

[+] [1;33mBoot and services[0m
------------------------------------
[2C- Service Manager[42C [ [1;32msystemd[0m ]
[2C- Checking UEFI boot[39C [ [1;37mDISABLED[0m ]
[2C- Checking presence GRUB2[34C [ [1;32mFOUND[0m ]
[4C- Checking for password protection[23C [ [1;31mNONE[0m ]
[2C- Check running services (systemctl)[23C [ [1;32mDONE[0m ]
[8CResult: found 35 running services[20C

这应该是一样的。

我正在使用的python代码:

output_lynis = subprocess.getoutput(to_execute)
print(output_lynis)

打印它是可以的,但将它保存到变量\文件会破坏它。我的问题显然是,我怎样才能正确保存它?(我在 ubuntu 中运行 lynis 并尝试保存输出)

标签: pythonlinuxubuntuterminal

解决方案


推荐阅读