在python中,您可以
sys.stdout = open('log', 'w') # begin redirect
然后输出将改为写入log
。
您可以使用以下方法恢复到正常状态
sys.stdout = sys.__stdout__ # end redirect, restore back
如何在zsh和bash中实现类似的效果?
P.S。,
ls > log
不是我想要的。为了澄清,我想要的是
ls # output to terminal
# begin redirect to `log`
ls # output to `log`
find -type f # output to `log`
... # output to `log`
# end redirect, restore back
ls # output to terminal
您可以使用tee
命令登录到文件以及打印到控制台:
ls # output to terminal
# begin redirect to `log`
ls | tee -a log # output to `log`
find -type f | tee -a log # output to `log`
... # output to `log`
# end redirect, restore back
ls # output to terminal