0

What I want to do

Go into the folder that the user selects, then show them the files from this folder

How I would like I do this

I would like to show the user a list of files and the folder they are contained in, and then allow the user to select one.

Code

I've tried this:

#!/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
Undo
  • 25,519
  • 37
  • 106
  • 129
  • 3
    What are `a` and `b` in this context? And by *listed item* do you mean the elements of `items` or the values you are printing? It's also worth noting counting in a `for` loop is a bad practice, instead, use [`enumerate()`](http://docs.python.org/3.3/library/functions.html#enumerate). – Gareth Latty May 12 '13 at 13:54
  • 'for i, item in enumerate(items):' instead of i = 1, and i+=1; make sure you change the print statement from i to i+1. – tmj May 12 '13 at 14:25

1 Answers1

1

Since items is a list, you can access it by its index:

items = ['a','b','c']
print(items[0])

That will print a. For your example:

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)))
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • also consider: `os.path.join` – jamylak May 12 '13 at 14:29
  • @EvženWybitul See [this question](http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python) – Burhan Khalid May 12 '13 at 14:49
  • Plz how can I use just the name of the chosen item (u know, the_path is path of the item, but can I get just the last one in the path - how to setup a variable to it). Cause the path must be /var/mobile/TP/*thechosenitem*/*thesamenameaschosenitem* – Evžen Wybitul May 13 '13 at 03:50