to save our time, u are free to jump to the bottom to see the conclusion
Start
I am searching for the answer to the question as u did.
At First, I think the following will solve the problem...
multiple piped inputs without EOF
How to use `input()` after reading a file from `stdin`?
however, it does not succeed in my computer,
and thus, I try to figure out how to exactly clear the EOF mark...
Terminate/Break/Abort Python Console/sys.stdin readline()
How do you terminate/break/abort a Python console/sys.stdin readline()?
flush
How to flush the input stream in python?
In C Language, we use fflush(stdin), ..etc to clear the buffer
In C++ Language, we use std::cin.get(), ..etc to clear the buffer
But How About Python Language ??
After Searching For 2 hours, I realize Python so far does not provide this function, clear the buffer/EOF in the stdin
And the answers available online which succeed like this one
How to use `input()` after reading a file from `stdin`? are only suitable for Linux System
After Reading This
https://www.twblogs.net/a/5c0ac824bd9eee6fb21399d4
*I am so sure(i think) that Python in Windows cannot use input after stdin is taken by file
However, does that mean, we should batter the sys.stdin method?
No, Obviously, or I won't spend my time here typing...
And Here Is My Final Method To Solve The Problem
re-Create Your Own Input as follow
def new_input(): ## version 1
import msvcrt
str_=''
c=msvcrt.getche()
while ord(c)!=3 and ord(c)!=4 and ord(c)!=26 and ord(c)!=13:
# print(ord(c))
str_ = str_+str(c)[2:-1]
c=msvcrt.getche()
return str_
def new_input(interact_string_): ## version 2
import msvcrt
print(interact_string_, end ="")
str_=''
c=msvcrt.getche()
while ord(c)!=3 and ord(c)!=4 and ord(c)!=26 and ord(c)!=13:
# print(ord(c))
str_ = str_+str(c)[2:-1]
c=msvcrt.getche()
return str_
def new_input(interact_string_): ## version 3
import msvcrt
import os
print(interact_string_, end ="")
str_=''
print('')
c=msvcrt.getche()
while ord(c)!=3 and ord(c)!=4 and ord(c)!=26 and ord(c)!=13:
os.system('cls')
print(interact_string_, end ="")
# print(ord(c))
if ord(c)==8:
str_ = str_[0:-1]
print(str_)
else:
str_ = str_+str(c)[2:-1]
print(str_)
c=msvcrt.getche()
# print(str_)
return str_
Here, are the meanings of numbers
- ord(c)==3 will be the key of Ctrl+C
- ord(c)==4 will be the key of Ctrl+D
- ord(c)==26 will be the key of Ctrl+Z
- ord(c)==13 will be the key of Enter
- ord(c)==22 will be the key of Ctrl+C
- ord(c)==8 will be the key of Backspace
And it will still be convenient to using if-structure to react with, and returns the final string
Conclusion:
after using stdin to readlines()/readline()/read() to read a file you cannot ever using stdin to input/output in Windows System
while, in the same case, the Same Problem on Linux System is solvable, and the answer will be up there...
In my opinion and under my research, The Best Answer to Windows will be mine (of course will I say that..) . ++==> re-Create your input()
like it if u enjoy the journey with me while reading my adventure to finding answer