如何将Python脚本的输出重定向到终端

0 投票
1 回答
997 浏览
提问于 2025-04-16 23:12

我该怎么把我的Python脚本输出重定向到Ubuntu下某个已经打开的终端窗口呢?这个脚本是通过KMail的过滤规则启动的。

1 个回答

1

创建一个简单的套接字服务器是一种方法……但我可能会使用管道(fifos):

$ mkfifo /tmp/my_fifo
$ cat producer.py
f = open("/tmp/my_fifo", "w")
f.write("hello, world!\n")
f.close()

然后你可以用 cat /tmp/my_fifo 来读取它的内容。

或者使用一个简单的日志文件:

$ cat producer.py
f = open("/tmp/my_log", "a")
f.write("hello, world!\n")
f.close()

然后你可以用 tail -f /tmp/my_log 来查看它的内容。

撰写回答