简体   繁体   English

如何使用stdout将打印消息的内容记录到两个文件中

[英]How do I log the contents of print messages to two files with stdout

Looking for some help logging/saving the prints to two file locations as seen below, does anyone know a way to do this? 在寻找一些帮助记录/保存打印到两个文件位置的帮助,如下所示,有人知道这样做的方法吗?

### Create output file/open it for editing
output_file = open('FILE.txt','w')
output_file1 = open('FILE_APPENDING.txt','a')

## Create a backup of current setting
old_stdout = sys.stdout

sys.stdout = output_file
sys.stdout = output_file1

print "stuff here"
## loop here printing stuff

## Revert python to show prints as normal
sys.stdout=old_stdout

## Close the file we are writing too
output_file.close()
output_file1.close()

Thanks in advance - Hyflex 在此先感谢-Hyflex

You can reassign sys.stdout with some class that writes to multiple files: 您可以使用一些写入多个文件的类来重新分配sys.stdout

class MultiWrite(object):
    def __init__(self, *files):
        self.files = files
    def write(self, text):
        for file in self.files:
            file.write(text)
    def close(self):
        for file in self.files:
            file.close()

import sys

# no need to save stdout. There's already a copy in sys.__stdout__.
sys.stdout = MultiWrite(open('file-1', 'w'), open('file-2', 'w'))
print("Hello, World!")

sys.stdout.close()

sys.stdout = sys.__stdout__  #reassign old stdout.

Anyway, I agree with Ashwini. 无论如何,我同意阿什维尼的观点。 It seems that you are searching a hack to obtain something, when you should really use a different approach. 看来您应该真正使用其他方法来搜索黑客,以获得某些东西。

Simply use file.write : 只需使用file.write

with open('FILE.txt','w')  as output_file:
    #do something here
    output_file.write(somedata) # add '\n' for a new line

with open('FILE_APPENDING.txt','a')  as output_file1:
    #do something here
    output_file1.write(somedata) 

help on file.write : 关于file.write帮助:

>>> print file.write.__doc__
write(str) -> None.  Write string str to file.

Note that due to buffering, flush() or close() may be needed before
the file on disk reflects the data written.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅打印给定记录器的日志消息? - How do I print out only log messages for a given logger? 将stdout和stderr重定向到两个不同的日志文件 - Redirection of stdout and stderr to two different log files 如何比较两个 Google Protocol Buffer 消息的内容是否相等? - How do I compare the contents of two Google Protocol Buffer messages for equality? 如何打印 a 的内容<cite>?</cite> - how do I print the contents of a <cite>? 如何防止 C 共享库在 python 的标准输出上打印? - How do I prevent a C shared library to print on stdout in python? Python - 所有print和stdout如何从终端获取以便我可以创建日志? - Python - all the print and stdout how can i get from terminal so that i can make a log? Python:记录器在登录到stdout和两个文件时会在屏幕上复制消息 - Python: logger duplicates messages on screen when logging to stdout and two files 如何以编程方式告诉 Celery 将所有日志消息发送到 stdout 或 stderr? - How to programmatically tell Celery to send all log messages to stdout or stderr? 如何将消息记录到通用(更改)文件中 - How can I log messages to generic (changing) files 守护程序后如何记录stdout? - How did I log stdout after daemonize?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM