2013-05-12 7 views
0

나는 사용자가 선택하는가,이 폴더에서 그들에게 파일을 표시 폴더로파이썬에서 나열된 항목에 변수를 할당하는 방법은 무엇입니까?

이동을 수행 할 작업을

나는이

싶습니다 할 싶습니다 방법 사용자에게 포함 된 파일 및 폴더의 목록을 표시 한 다음 사용자가 파일 및 폴더를 선택할 수 있도록 허용합니다.

나는이 시도했습니다

코드 : items 이후

#!/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 
+3

이 컨텍스트에서 'a' 및'b'는 무엇입니까? 그리고 * listing item *에 의해'items' 요소 나 인쇄중인 값을 의미합니까? 'for' 루프를 세는 것은 나쁜 습관 일뿐만 아니라 ['enumerate()'] (http://docs.python.org/3.3/library/functions.html#enumerate)를 사용하는 것도 중요합니다. –

+0

'for i, enumerate (items)의 항목 :'대신 i = 1, i + = 1; print 문을 i에서 i + 1로 변경하십시오. – tMJ

답변

1

이 목록입니다, 당신은 그 인덱스로 액세스 할 수 있습니다 :

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))) 
+0

도 고려해보십시오 :'os.path.join' – jamylak

+0

@ EvženWybitul See [이 질문] (http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python) –

+0

Plz 선택한 항목의 이름 만 사용하려면 어떻게해야합니까? (예를 들어, the_path는 항목의 경로입니다.하지만 마지막으로 경로에있는 항목을 가져올 수 있습니다. 예를 들어 변수를 설정하는 방법). 경로가/var/mobile/TP/*이어야합니다. */thechosenitem */* thesamenameaschosenitem * –

관련 문제