在sys.stdout中使用Python'with'语句

far*_*mir 8 python stdout stream with-statement

我总是使用with语句打开并写入文件:

with open('file_path', 'w') as handle:
    print >>handle, my_stuff
Run Code Online (Sandbox Code Playgroud)

但是,有一个实例,我需要能够更灵活,并写入sys.stdout(或其他类型的流),如果提供而不是文件路径:

所以,我的问题是:有没有办法使用with真实文件和使用语句sys.stdout

请注意,我可以使用以下代码,但我认为这违背了使用目的with:

if file_path != None:
    outputHandle = open(file_path, 'w')
else:
    outputHandle = sys.stdout

with outputHandle as handle:
    print >>handle, my_stuff
Run Code Online (Sandbox Code Playgroud)

the*_*eye 8

您可以创建一个上下文管理器并像这样使用它

import contextlib, sys

@contextlib.contextmanager
def file_writer(file_name = None):
    # Create writer object based on file_name
    writer = open("Output.txt", "w") if file_name is not None else sys.stdout
    # yield the writer object for the actual use
    yield writer
    # If it is file, then close the writer object
    if file_name != None: writer.close()

with file_writer("Output.txt") as output:
    print >>output, "Welcome"

with file_writer() as output:
    print >>output, "Welcome"
Run Code Online (Sandbox Code Playgroud)

如果你没有传递任何输入,file_writer它将使用sys.stdout.

  • 我会将 `!=` 替换为 `is not`。 (2认同)

sle*_*ica 4

事实是,您不需要将上下文处理器与 一起使用stdout,因为您没有打开或关闭它。一种不太花哨的抽象方法是:

def do_stuff(file):
    # Your real code goes here. It works both with files or stdout
    return file.readline()

def do_to_stdout():
    return do_stuff(sys.stdout)

def do_to_file(filename):
    with open(filename) as f:
        return do_stuff(f)


print do_to_file(filename) if filename else do_to_stdout()
Run Code Online (Sandbox Code Playgroud)