1

When I try some cmd | main.py:

if not sys.stdin.isatty():
    input() # how to wait for user input but not from pipe?

I am already read Python script not waiting for user input when ran from piped bash script, but I cannot found /dev/tty in windows and handle it in python code.

2
  • If you call it like that, the pipe is connected to stdin of your python script, not the terminal. The user's input goes to some cmd. Commented Sep 3, 2016 at 4:28
  • @KlausD. It must be some way to get /dev/tty as stdin direct. Commented Sep 3, 2016 at 5:32

1 Answer 1

1

I cannot found /dev/tty in Windows and handle it in python code. It could be CON or CON:

Here's my python script which accepts any combination of inputs from pipeline and from command line arguments as well as user's input from console:

#! 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)

Output:

dir /b py*.py | python_stdin.py a "b & c"
 … your input (`Enter` to quit) … foo
 …                              … bar
 …                              …
3 arguments: ['D:\\bat\\python_stdin.py', 'a', 'b & c']
2 pipelines: ['pythonprint.py', 'python_stdin.py']
2 kbd input: ['foo', 'bar']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.