【发布时间】:2021-08-27 03:21:57
【问题描述】:
我的python脚本可以在两种模式下使用:
./foo arg file
或
cat file | foo arg
这是我决定标准输入是否是管道的方式:
if sys.stdin.isatty():
print('not pipe\n')
else:
print('is pipe')
它的行为符合预期:
./foo arg file
not pipe
cat file | ./foo arg
is pipe
但是,在以下用法中,它认为输入来自管道,即使管道只是 while 循环的一部分:
while read F ; do foo arg $F ; done < /tmp/zz
is pipe
我将我的脚本称为./foo arg file。为什么它认为输入是管道?
【问题讨论】:
标签: python shell while-loop stdin