5.18 如何将 print 内容输出到文件

http://image.iswbm.com/20200804124133.png

Python 3 中的 print 作为一个函数,由于可以接收更多的参数,所以功能变为更加强大。

比如今天要说的使用 print 将你要打印的内容,输出到日志文件中(但是我并不推荐使用它)。

>>> with open('test.log', mode='w') as f:
...     print('hello, python', file=f, flush=True)
>>> exit()

$ cat test.log
hello, python