Python 非'sys.stdin.isatty()时等待用户输入`

Python 非'sys.stdin.isatty()时等待用户输入`,python,Python,当我尝试使用一些cmd | main.py时: if not sys.stdin.isatty(): input() # how to wait for user input but not from pipe? 我已经读过了,但是我在windows中找不到/dev/tty,在python代码中处理它我在windows中找不到/dev/tty,在python代码中处理它。它可以是CON或CON: 下面是我的python脚本,它接受来自管道和命令行参数的任何输入组合,以及来自控制台的用户

当我尝试使用一些cmd | main.py时:

if not sys.stdin.isatty():
    input() # how to wait for user input but not from pipe?
我已经读过了,但是我在windows中找不到
/dev/tty
,在python代码中处理它

我在windows中找不到
/dev/tty
,在python代码中处理它。它可以是
CON
CON:

下面是我的python脚本,它接受来自管道命令行参数的任何输入组合,以及来自控制台的用户输入

#! python3
# -*- coding: utf-8 -*- 

import sys

argv_tuple = []                                   # command line arguments
for arg in sys.argv:
    argv_tuple.append( arg )

pipe_tuple = []                                   # values from pipeline
if not sys.stdin.isatty():
    for line in sys.stdin:
        pipe_tuple.append( line.replace('\n', '').replace('\r','') )
    sys.stdin = open('CON:', mode='r', encoding=sys.stdin.encoding)

inpt_tuple = []                                   # inputs from the console
invalue = input(' … your input (`Enter` to quit) … ')
while not invalue == '':                  
    inpt_tuple.append( invalue)
    invalue = input(' …                              … ')

#                                                 # publish the results
print( len( argv_tuple), 'arguments:', argv_tuple)
print( len( pipe_tuple), 'pipelines:', pipe_tuple)
print( len( inpt_tuple), 'kbd input:', inpt_tuple)
dir /b py*.py | python_stdin.py a "b & c"
输出

#! python3
# -*- coding: utf-8 -*- 

import sys

argv_tuple = []                                   # command line arguments
for arg in sys.argv:
    argv_tuple.append( arg )

pipe_tuple = []                                   # values from pipeline
if not sys.stdin.isatty():
    for line in sys.stdin:
        pipe_tuple.append( line.replace('\n', '').replace('\r','') )
    sys.stdin = open('CON:', mode='r', encoding=sys.stdin.encoding)

inpt_tuple = []                                   # inputs from the console
invalue = input(' … your input (`Enter` to quit) … ')
while not invalue == '':                  
    inpt_tuple.append( invalue)
    invalue = input(' …                              … ')

#                                                 # publish the results
print( len( argv_tuple), 'arguments:', argv_tuple)
print( len( pipe_tuple), 'pipelines:', pipe_tuple)
print( len( inpt_tuple), 'kbd input:', inpt_tuple)
dir /b py*.py | python_stdin.py a "b & c"
我无法在Windows中找到
/dev/tty
,也无法在python代码中处理它。它可以是
CON
CON:

下面是我的python脚本,它接受来自管道命令行参数的任何输入组合,以及来自控制台的用户输入

#! python3
# -*- coding: utf-8 -*- 

import sys

argv_tuple = []                                   # command line arguments
for arg in sys.argv:
    argv_tuple.append( arg )

pipe_tuple = []                                   # values from pipeline
if not sys.stdin.isatty():
    for line in sys.stdin:
        pipe_tuple.append( line.replace('\n', '').replace('\r','') )
    sys.stdin = open('CON:', mode='r', encoding=sys.stdin.encoding)

inpt_tuple = []                                   # inputs from the console
invalue = input(' … your input (`Enter` to quit) … ')
while not invalue == '':                  
    inpt_tuple.append( invalue)
    invalue = input(' …                              … ')

#                                                 # publish the results
print( len( argv_tuple), 'arguments:', argv_tuple)
print( len( pipe_tuple), 'pipelines:', pipe_tuple)
print( len( inpt_tuple), 'kbd input:', inpt_tuple)
dir /b py*.py | python_stdin.py a "b & c"
输出

#! python3
# -*- coding: utf-8 -*- 

import sys

argv_tuple = []                                   # command line arguments
for arg in sys.argv:
    argv_tuple.append( arg )

pipe_tuple = []                                   # values from pipeline
if not sys.stdin.isatty():
    for line in sys.stdin:
        pipe_tuple.append( line.replace('\n', '').replace('\r','') )
    sys.stdin = open('CON:', mode='r', encoding=sys.stdin.encoding)

inpt_tuple = []                                   # inputs from the console
invalue = input(' … your input (`Enter` to quit) … ')
while not invalue == '':                  
    inpt_tuple.append( invalue)
    invalue = input(' …                              … ')

#                                                 # publish the results
print( len( argv_tuple), 'arguments:', argv_tuple)
print( len( pipe_tuple), 'pipelines:', pipe_tuple)
print( len( inpt_tuple), 'kbd input:', inpt_tuple)
dir /b py*.py | python_stdin.py a "b & c"

如果这样调用,则管道连接到python脚本的stdin,而不是终端。用户的输入转到
some cmd
@klauds。必须通过某种方式将
/dev/tty
作为stdindirect获取。如果您这样调用它,则管道连接到python脚本的stdin,而不是终端。用户的输入转到
some cmd
@klauds。它必须以某种方式获得stdindirect的
/dev/tty