用于python的Unix tee命令
pyteetime的Python项目详细描述
Unix命令tee
分割程序的输出,以便两者兼而有之
显示在屏幕上并写入文件。pyteetime
包
在Python中提供这样的功能。它是以原作为基础的
来自A.
Peck的代码,带有
添加了一些类方法。在
用法:
运行下面列出的示例脚本tee-test.py
:
frompyteetimeimportteeimportsysprint('This prologue will appear on screen but not in a logfile')LOGFILE=tee.stdout_start(append=False)# STDOUT# from now on, all output is also copied to the logfiletee.stderr_start(append=False)# STDERR# from now on, all output to STDERR is also copied to stderr.logprint('This text will appear on screen and also in the logfile')print('This will appear on screen and also in stderr.log',file=sys.stderr)# input from keyboard does not go to logfile:answer=input('Enter something!\n')# show the input to make sure it also goes into the logfile:print('The user typed: %s'%(answer))# data written to a file is not copied to the logfile:DATAFILE=open('tee-test.dat','w+')print(list(range(5)),file=DATAFILE)DATAFILE.close()print('This goes to the logfile but will not appear on screen',file=LOGFILE)tee.stdout_stop()# from now on, output to STDOUT will not go to stdout.log anymoretee.stderr_stop()# from now on, output to STDERR will not go to stderr.log anymoreprint('This epilogue will appear on screen but not in a logfile')
- 项目
标签: