Determine if standard output is piped when running a Python script

Use isatty to determine if standard output is piped at runtime --Qiita

I tried to do this with Python, so I made a note.

isatty.py


import sys

print(sys.stdout.isatty())

When executed, it becomes like this

$ python isatty.py
True
$ python isatty.py | cat
False

Supplement

This looks good

isatty.py


import os, sys

print(os.isatty(sys.stdout.fileno()))

Recommended Posts

Determine if standard output is piped when running a Python script
Output timing is incorrect when standard (error) output is converted to a file in Python
What's in that variable (when running a Python script)
Determine if a string is a time with a python regular expression
Trap trapped when running a Python Windows executable
[Grasshopper] When creating a data tree on Python script
Check if the string is a number in python
Create a python script to check if the link at the specified URL is valid 2
Create a python script to check if the link at the specified URL is valid
A convenient function memo to use when you want to enter the debugger if an error occurs when running a Python script.
python> check NoneType or not> if a == None:> if a is None:
Use communicate () when receiving output in a Python subprocess
When converting a string (a-> b) in Python, which is faster, if statement or dictionary type?
Change the standard output destination to a file in Python
A memo when face is detected with Python + OpenCV quickly
[GCP] A memorandum when running a Python program on Cloud Functions
Python list is not a list
Specify options when running python
What is a python map?
Write a script in Shell and Python to notify you in Slack when the process is finished
Convert to a string while outputting standard output with Python subprocess
Python will fail if there is a space after the backslash
[Blender] Script to check if the selected one is a mesh
[Python] Execution time when a function is entered in a dictionary value
Delete a particular character in Python if it is the last
Read the standard output of a subprocess line by line in Python
A note on using tab completion when running Python interactively on Windows
How to determine if a shell script was started in bash
A memo for when pip3 is installed with python2.7 for some reason
[Python] What is a zip function?
[Python] What is a with statement?
A note when gcloud is broken
Write standard output to a file
Make standard output non-blocking in Python
Determine if the string is formatable
Determine if the library is installed.
Determine if AWS Chalice is chalice local
Determining which OS is running Python
Launch a Python script as a service
Write a batch script with Python3.5 ~
When writing a program in Python
What to do if there is a decimal in python json .dumps
When running a Python shell from Electron, pass multiple arguments to run Python.
A memorandum when Django is installed on Sakura's rental server (standard plan)
I created a script to check if English is entered in the specified position of the JSON file in Python.