python - 如何使用用户输入继续目录中的文件?
问题描述
我的代码是:
print("I have found the following files:")
initial_count = 0
direc = os.path.expanduser('~/Documents/GIT/python_adb/apks/')
for file in os.listdir(direc):
initial_count += 1
files = print('[', initial_count, ']' , file)
files
我的输出:
I have found the following files:
[ 1 ] Kik.apk
[ 2 ] vlc_for_android.apk
现在我想问用户他想使用哪个文件,比如:
apk_choice = input("Which apk do yo want to install? Type in the number of the file like 1 or 2 and press enter.")
我如何使用用户输入(例如他输入 1 并按下回车)来使用 [1] Kik.apk?
那么如何将他的输入与文件进行比较并继续选择的文件呢?
解决方案
我会使用文件列表和给定的数字作为索引:
print("I have found the following files:")
initial_count = 0
direc = os.path.expanduser('~/Documents/GIT/python_adb/apks/')
files = os.listdir(direc)
for file in files:
initial_count += 1
print('[', initial_count, ']' , file)
inp = input(">>>")
print(files[int(inp)-1])
注意:您必须从输入中减去 1,因为 Python 中的列表是基于 0 的索引。
推荐阅读
- amazon-web-services - 跟踪 AWS ApiGateway 中的完整请求/响应主体(未截断)
- react-native - React Native 使用 Twitter 搜索 API 和仅应用程序身份验证
- arrays - 在表格角度显示来自 JSON 的嵌套数据
- python - Python上升/下降沿示波器类触发器
- java - 滚动视图在 OS 4.1.2 上不起作用
- android - Android Facebook 专辑封面照片
- sql-server - 为什么 T-SQL 不会因错误而停止
- python - 如何在 Flask Python 的 WSGIServer 中启用/实现多线程
- r - 更新优化问题的下限和上限
- c# - 使用 C# 编辑 PDF 文本