如何使用用户输入继续与目录中的文件?


我的代码是:
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的索引。

相关内容

  • 没有找到相关文章

最新更新