linux tee没有使用python?

dae*_*hee 87 python linux tee

我制作了一个python脚本,它使用无限循环与Web服务器通信.我想将每个通信数据记录到一个文件中,并同时从终端监视它们.所以我用这样的tee命令.

python client.py | tee logfile
Run Code Online (Sandbox Code Playgroud)

但是,我从终端或日志文件中得不到任何东西.python脚本工作正常.这里发生了什么?我错过了什么吗?

一些建议将不胜感激.先感谢您.

Vor*_*Vor 157

来自man python:

   -u     Force stdin, stdout and stderr to  be  totally  unbuffered.   On  systems
          where it matters, also put stdin, stdout and stderr in binary mode.  Note
          that there is internal buffering in xreadlines(), readlines()  and  file-
          object  iterators  ("for  line  in sys.stdin") which is not influenced by
          this option.  To work around this, you will want to use  "sys.stdin.read?
          line()" inside a "while 1:" loop.
Run Code Online (Sandbox Code Playgroud)

所以你能做的是:

/usr/bin/python -u client.py >> logfile 2>&1
Run Code Online (Sandbox Code Playgroud)

或使用tee:

python -u client.py | tee logfile
Run Code Online (Sandbox Code Playgroud)

  • 谢谢我把它添加到顶部:#!/ usr/bin/python -u (13认同)