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 に答える