Python的解释器默认启用输出缓冲sys.stdout吗?
如果答案是肯定的,那么禁用它的所有方法是什么?
建议到目前为止:
-u命令行开关sys.stdout在每次写入后刷新的对象PYTHONUNBUFFEREDenv varsys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)是否有任何其他方式来设置一些全局标志sys/ sys.stdout程序执行过程中?
我希望能够将iPython笔记本单元的TEXT输出保存到磁盘上的文件中.
我有2个额外的要求/要求:
我已经想出如何使用%%capture魔法将iPython笔记本电脑的单元基本保存到一个文件中,但它看起来不够灵活:每次重新运行单元格时它都会不断添加,我无法在其中显示相同的细胞.
这是我到目前为止:
%%capture cap --no-stderr
print 'stuff'
with open('output.txt', 'w') as f:
f.write(cap.stdout)
# clear the cap by deleting the variable here?
# del cap
Run Code Online (Sandbox Code Playgroud)
当我尝试cap.show()写入后,它似乎没有显示.相反,它将输出放入cap变量两次.
我有以下日志记录设置:
import logging
logging.basicConfig(level=logging.DEBUG,
format='[%(asctime)s] - %(funcName)s - %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='/tmp/affiliate.log',
filemode='w')
Run Code Online (Sandbox Code Playgroud)
我如何转换以下两个语句 - 写入日志文件然后打印输出:
logging.debug('>>>>> 401 Error: Cannot download file')
print '>>>>> 401 Error: Cannot download file'
Run Code Online (Sandbox Code Playgroud)
在一个日志语句中同时执行两个操作