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

How to use users input continue with file in directory?

我的代码是:

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 的索引。