`with open (…)`과`sys.stdout`을 모두 멋지게 처리하는 방법은 무엇입니까? stdout에 데이터를 출력해야합니다. 다음 스 니펫을 사용합니다. if

종종 파일 또는 파일이 지정되지 않은 경우 stdout에 데이터를 출력해야합니다. 다음 스 니펫을 사용합니다.

if target:
    with open(target, 'w') as h:
        h.write(content)
else:
    sys.stdout.write(content)

다시 작성하고 두 대상을 균일하게 처리하고 싶습니다.

이상적인 경우는 다음과 같습니다.

with open(target, 'w') as h:
    h.write(content)

그러나 with블록을 떠날 때 sys.stdout이 닫히고 원하지 않기 때문에 이것은 잘 작동 하지 않습니다. 나는 원하지 않는다

stdout = open(target, 'w')
...

원래 stdout을 복원하는 것을 기억해야하기 때문입니다.

관련 :

편집하다

랩핑 target하거나 별도의 기능을 정의하거나 컨텍스트 관리자를 사용할 수 있다는 것을 알고 있습니다 . 5 줄 이상 필요하지 않은 간단하고 우아하며 관용적 인 솔루션을 찾습니다.



답변

여기서 상자 밖에서 생각하면 사용자 지정 open()방법은 어떻습니까?

import sys
import contextlib

@contextlib.contextmanager
def smart_open(filename=None):
    if filename and filename != '-':
        fh = open(filename, 'w')
    else:
        fh = sys.stdout

    try:
        yield fh
    finally:
        if fh is not sys.stdout:
            fh.close()

다음과 같이 사용하십시오.

# For Python 2 you need this line
from __future__ import print_function

# writes to some_file
with smart_open('some_file') as fh:
    print('some output', file=fh)

# writes to stdout
with smart_open() as fh:
    print('some output', file=fh)

# writes to stdout
with smart_open('-') as fh:
    print('some output', file=fh)


답변

현재 코드를 고수하십시오. 간단하고 흘끗 쳐다 보는 것만으로 정확히 무엇을하는지 알 수 있습니다 .

또 다른 방법은 인라인을 사용하는 것입니다 if.

handle = open(target, 'w') if target else sys.stdout
handle.write(content)

if handle is not sys.stdout:
    handle.close()

그러나 그것은 당신이 가진 것보다 훨씬 짧지 않으며 틀림없이 더 나빠 보입니다.

sys.stdout닫을 수 없게 만들 수도 있지만 너무 파이썬 적으로 보이지는 않습니다.

sys.stdout.close = lambda: None

with (open(target, 'w') if target else sys.stdout) as handle:
    handle.write(content)


답변

EAFP를 할 수 있는데 왜 LBYL인가?

try:
    with open(target, 'w') as h:
        h.write(content)
except TypeError:
    sys.stdout.write(content)

복잡한 방식으로 작동해야 할 때 with/ as블록을 균일하게 사용하도록 다시 작성하는 이유는 무엇 입니까? 더 많은 선을 추가 하고 성능을 저하시킵니다.


답변

또 다른 가능한 해결책 : 컨텍스트 관리자 종료 방법을 피하지 말고 stdout을 복제하십시오.

with (os.fdopen(os.dup(sys.stdout.fileno()), 'w')
      if target == '-'
      else open(target, 'w')) as f:
      f.write("Foo")


답변

Wolph의 답변 개선

import sys
import contextlib

@contextlib.contextmanager
def smart_open(filename: str, mode: str = 'r', *args, **kwargs):
    '''Open files and i/o streams transparently.'''
    if filename == '-':
        if 'r' in mode:
            stream = sys.stdin
        else:
            stream = sys.stdout
        if 'b' in mode:
            fh = stream.buffer  # type: IO
        else:
            fh = stream
        close = False
    else:
        fh = open(filename, mode, *args, **kwargs)
        close = True

    try:
        yield fh
    finally:
        if close:
            try:
                fh.close()
            except AttributeError:
                pass

이것은 바이너리 IO를 허용하고 openif filename가 실제로 파일 이름 인 경우에 최종 외부 인수를 전달 합니다.


답변

또한 모드 (결과적으로 stdin 대 stdout)를 무시할 수있는 경우 매우 간단 할 수있는 간단한 래퍼 함수를 ​​사용합니다. 예를 들면 다음과 같습니다.

from contextlib import contextmanager
import sys

@contextmanager
def open_or_stdout(filename):
    if filename != '-':
        with open(filename, 'w') as f:
            yield f
    else:
        yield sys.stdout


답변

좋아, 우리가 한 줄로 전쟁을 시작한다면, 여기에 :

(target and open(target, 'w') or sys.stdout).write(content)

나는 맥락이 한곳에서만 쓰여지는 한 Jacob의 원래 예를 좋아합니다. 많은 쓰기를 위해 파일을 다시 열면 문제가됩니다. 스크립트 상단에서 한 번만 결정을 내리고 종료시 시스템이 파일을 닫도록 할 것이라고 생각합니다.

output = target and open(target, 'w') or sys.stdout
...
output.write('thing one\n')
...
output.write('thing two\n')

더 깔끔하다고 생각되면 자체 종료 핸들러를 포함 할 수 있습니다.

import atexit

def cleanup_output():
    global output
    if output is not sys.stdout:
        output.close()

atexit(cleanup_output)