嘿我正在研究一个需要几分钟动作的python项目。事情是,因为它需要几分钟,我希望用户能够按Enter键以查看操作的当前状态。我怎么能在Python 2中做到这一点?
答案 0 :(得分:0)
@ Space_C0wb0y是对的,进度条是一个很好的解决方案。但是,这证明了一种方法可以满足您的要求。有些代码来自此处:Non-blocking read on a subprocess.PIPE in python
import fcntl, os, sys
# make stdin a non-blocking file
fd = sys.stdin.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
def enter_pressed():
data = sys.stdin.read(1)
return bool(data)
i = 0
while True:
i += 1
if enter_pressed():
print(i)