import sys stdinfileno = sys.stdin Keeps reading from stdin and quits only if the word…
Jump to turn (11)
- 1import sys stdinfileno = sys.stdin Keeps reading from stdin and quits only if th
- 2please illustrate the above by code details
- 3how to write a string to a stdin of subprocess and subprocess detects and invoke
- 4Write the string to the subprocess’ stdin p.stdin.write(‘stringtowrite\n’) p.std
- 5what is input()?
- 6logically this is incorrect, input is working only once and if we further send s
- 7so input reads from stdin ?
- 8but i want the subprocess waiting for input for infinite times
- 9what is p.communicate() why should we wait and how it determine what the wait is
- 10but our subprocess is never terminated it is constantly waiting for input and pr
- 11how to read the stdout of the subprocess when subprocess is not terminated
- User
import sys
stdin_fileno = sys.stdin
Keeps reading from stdin and quits only if the word 'exit' is there
This loop, by default does not terminate, since stdin is open
for line in stdin_fileno: # Remove trailing newline characters using strip() if 'exit' == line.strip(): print('Found exit. Terminating the program') exit(0) else: print('Message from sys.stdin: ---> {} <---'.format(line)) what is the type of stdin_fileno and why the for loop here never ends
- Assistant
The variable
stdin_filenois of type<class '_io.TextIOWrapper'>, which is the input/output text wrapper class for accessing streams (in this case, stdin).The for-loop here never ends by default because stdin is an open input stream that continuously waits for input until it is closed. Therefore, the loop keeps iterating over incoming lines of input from stdin until it encounters an input line containing the string 'exit', which is when it terminates the program using .