Programing

sys.stdout이 터미널에 연결되어 있는지 여부를 어떻게 감지합니까?

lottogame 2020. 7. 10. 08:15
반응형

sys.stdout이 터미널에 연결되어 있는지 여부를 어떻게 감지합니까? [복제]


이 질문에는 이미 답변이 있습니다.

sys.stdout콘솔 터미널에 연결되어 있는지 여부를 감지하는 방법이 있습니까? 예를 들어 foo.py가 다음을 통해 실행되는지 감지하고 싶습니다.

$ python foo.py  # user types this on console

또는

$ python foo.py > output.txt # redirection
$ python foo.py | grep ....  # pipe

이 질문을하는 이유는 진행률 표시 줄이 이전 경우 (실제 콘솔)에서만 발생하기를 원하기 때문입니다.


if sys.stdout.isatty():
    # You're running in a real terminal
else:
    # You're being piped or redirected

참고 URL : https://stackoverflow.com/questions/1077113/how-do-i-detect-whether-sys-stdout-is-attached-to-terminal-or-not

반응형