我正在尝试处理文件的内容。我需要关于如何与他人互动的想法 用户并等待按下某个键将当前行写入另一个文件。预先感谢您。
with open(srcFile) as f:
line = f.readline().strip()
#- do something with line
#- part where I need help
if s is press, write the current line to file
if s is not pressed after 5 seconds, continue with the next line in srcFile
最佳答案
对于这种情况,您可以使用 time
和 keyboard
包:
import time
import keyboard
start_time = time.time() # Start the timer
key_pressed = False
while True:
if keyboard.is_pressed("s"):
key_pressed = True
break
if time.time() - start_time >= 5:
break
if key_pressed:
# write the current line to file
您可以使用上面的代码进行测试,看看效果。
关于python - 如何在python中等待用户输入5秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77063818/