Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检查文件是否写入终端?_Python_Unix - Fatal编程技术网

Python 如何检查文件是否写入终端?

Python 如何检查文件是否写入终端?,python,unix,Python,Unix,是否有某种方法可以检查python进程的输出是否正在写入文件?我希望能够做到以下几点: if is_writing_to_terminal: sys.stdout.write('one thing') else: sys.stdout.write('another thing') 使用os.isatty。这需要一个文件描述符(fd),它可以通过fileno成员获得 >>> from os import isatty >>> isatty(s

是否有某种方法可以检查python进程的输出是否正在写入文件?我希望能够做到以下几点:

if is_writing_to_terminal:
    sys.stdout.write('one thing')
else: 
    sys.stdout.write('another thing')

使用
os.isatty
。这需要一个文件描述符(fd),它可以通过
fileno
成员获得

>>> from os import isatty
>>> isatty(sys.stdout.fileno())
True
如果要支持任意类文件(例如
StringIO
),则必须检查类文件是否具有关联的fd,因为并非所有类文件都具有:

hasattr(f, "fileno") and isatty(f.fileno())

您可以使用
os.isatty()
检查文件描述符是否为终端:

if os.isatty(sys.stdout.fileno()):
    sys.stdout.write('one thing')
else: 
    sys.stdout.write('another thing')

嗯,
os.isatty(sys.stdout.fileno())
和?