在Python中记录终端输出

0 投票
1 回答
1234 浏览
提问于 2025-04-18 10:53

我在想,能不能用Python来捕捉终端的输出。下面是一个示例输出,如果我说得不太清楚的话:

person@computer:ls
    stuff stuff stuff
person@computer:python program.py
    person@computer:ls
    stuff stuff stuff
    person@computer:python program.py

1 个回答

0

我也同意@Carpetsmoker的看法。使用'script'这个命令可以帮你记录终端屏幕上的内容,不过它是一个UNIX命令,而不是Python脚本。你可以这样来使用它。

person@computer: script screen.log
-----Do some commands -----
person@computer: exit

当你退出时,会在你运行这个命令的文件夹里生成一个文本文件(screen.log),你可以查看这个文件。关于'script'命令的更多信息可以在这里找到:http://unixhelp.ed.ac.uk/CGI/man-cgi?script

撰写回答