0

我想做的事

进入用户选择的文件夹,然后向他们展示该文件夹中的文件

我多么希望我这样做

我想向用户显示文件列表和它们所在的文件夹,然后允许用户选择一个。

代码

我试过这个:

#!/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
4

1 回答 1

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)))
于 2013-05-12T13:56:57.483 回答