我正在编写一个脚本,需要在脚本中显示是-否提示,该提示要求用户以 y 或 n 的形式输入键盘
这是一个代码段:
import msvcrt,os
print("====> Creating Folders in the root folder...\n")
# while True:
d1 = str(input("Enter the folder name you want to create : "))
os.mkdir(os.path.join(root_dir,d1))
print("Do you want to create another folder,proceed(y/n)? : ",end='',flush=True)
key = msvcrt.getwch()
我尝试过的方法:
我发现,仅当我在CMD控制台上运行该文件时,Python的MSVCRT库才起作用。此外,它会捕获除 y 或 n 以外的任何键,还有更多的代码行会为其他输入引发错误。
我也尝试过使用readchar
库,但它也与msvcrt
相同。
我的期望:
1)脚本不仅应在CMD控制台上运行,而且应在交互式python控制台上运行良好。
2)我正在寻找一个python库,该库会阻止除 y 和 n 键之外的所有其他输入,因此无需管理来自的多余输入用户。
高度赞赏有关该主题的任何指导和意见。请随时提出任何疑问。
谢谢!
操作系统: Windows 10 x64
Python版本: 3.7.5(Anaconda)