Skip to content Skip to sidebar Skip to footer

Input() After Readlines() From Sys.stdin?

I have a case where my script calls readlines() on sys.stdin followed by a call to input(), but that last call won't work. Here's my script: import click @click.command() @click.a

Solution 1:

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 oneHow 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

defnew_input(): ## version 1import msvcrt
str_=''
c=msvcrt.getche()
whileord(c)!=3andord(c)!=4andord(c)!=26andord(c)!=13:
    # print(ord(c))
    str_ = str_+str(c)[2:-1]
    c=msvcrt.getche()
return str_

defnew_input(interact_string_): ## version 2import msvcrt
print(interact_string_, end ="")
str_=''
c=msvcrt.getche()
whileord(c)!=3andord(c)!=4andord(c)!=26andord(c)!=13:
    # print(ord(c))
    str_ = str_+str(c)[2:-1]
    c=msvcrt.getche()
return str_

defnew_input(interact_string_): ## version 3import msvcrt
import os
print(interact_string_, end ="")
str_=''print('')
c=msvcrt.getche()
whileord(c)!=3andord(c)!=4andord(c)!=26andord(c)!=13:
    os.system('cls')
    print(interact_string_, end ="")
    # print(ord(c))iford(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:

  1. after using stdin to readlines()/readline()/read() to read a file you cannot ever using stdin to input/output in Windows System

  2. while, in the same case, the Same Problem on Linux System is solvable, and the answer will be up there...

  3. 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()

Thanks For Your Reading, constructive advice and polite comment is welcomed. Anyone With a Better Idea or Solve is welcome too.

like it if u enjoy the journey with me while reading my adventure to finding answer

Post a Comment for "Input() After Readlines() From Sys.stdin?"