我想做什么
进入用户选择的文件夹,然后显示该文件夹中的文件
我希望如何做到这一点
我想向用户显示文件列表及其包含的文件夹,然后允许用户选择一个。
代码
我试过这个:
#!/usr/bin/env python
import os, sys, glob
os.chdir(glob.glob("/var/mobile/TP/")[0])
print("Please select a Texture Pack (##):")
items = os.listdir(".")
i = 1
for item in items:
print("[%.2d] %s" % (i, item))
i += 1
#And then something that will assign the variables to the listed items, like
*firstlisteditem*=a ; *secondlisteditem*=b #... and so on
#and then i need to get the user to tell me which folder he wants to go to. This I will do using a number.
se=raw_input ()
if "1" in se then
exec /var/mobile/TP/$a
if "2" in se then
exec /var/mobile/TP/$b
答案 0 :(得分:1)
由于items
是一个列表,您可以通过索引访问它:
items = ['a','b','c']
print(items[0])
这将打印a
。以你的例子:
se=int(raw_input())
if se < len(items):
item = items[se-1]
the_path = "/var/mobile/TP/{}".format(item)
else:
print("Please enter a value between 1 and {}".format(len(items)))