0

I want to select a file by numbers at left side of listing but I can't go more than this:

import os
path="/root/Desktop"
dirList=os.listdir(path)
for fname in dirList:
    print fname

selected = raw_input("Select a file above: ")

What should I do?

Example:

http://img502.imageshack.us/img502/4407/listingy.png

Thank you in advance..

6 Answers 6

2

You should use enumerate for the list and then handle input errors. Ideally this would be a function and instead of doing a break you would just return the selected file.

import os

path="/root/Desktop"
dirList=os.listdir(path)

for i, fname in enumerate(dirList):
    print "%d) %s" % (i + 1, fname)

while True:
    try:
        selectedInt = int(raw_input("Select a file above: "))
        selected = dirList[selectedInt - 1]
        break
    except Exception:
        print "Error: Please enter a number between 1 and %d" % len(dirList)
Sign up to request clarification or add additional context in comments.

Comments

1
for i, fname in enumerate(dirList):
    print "%s) %s" % (i + 1, fname)

selectedInt = int(raw_input("Select a file above: "))
selected = dirList[selectedInt - 1]

However, note that there is no error correction done. You should catch cases, where the input is no integer.

Comments

0

You could try the following :

import os
path="/root/Desktop"
dirList=os.listdir(path)
for i in range(0,len(dirList)): # generate an index an loop over it
    print "%d)" % (i+1), dirList[i] # print a selection number matching each file

selected = raw_input("Select a file above: ")
selected = int(selected) # cast the input to int

print "You have selected:", dirList[selected-1] # you can get the corresponding entry!

That should do the trick :)

1 Comment

@Boldewyn has done well to use enumerate so as to generate the index in the same time the list is iterated over.
0

You know the filename at a specific index so you can just do something like this:

import os
path="/root/Desktop"
dirList=os.listdir(path)

for index in range(0,len(dirList)):
    print str(index+1) + ": " + dirList[index]

selected = raw_input("Select a file above: ")
print "You selected filename: " + dirList[selected];

edit: oops to slow

1 Comment

Fixed a part and it works flawless. Thank you. --- print "You selected filename: " + dirList[int(selected)-1] ---
0

Try this:

import os

listed_files = []
for i, fname in enumerate(os.listdir("/root/Desktop")):
    print i, ': ', fname
    listed_files.append(fname)

selected = raw_input("Select a file above: ")
open("/root/Desktop/%s" % listed_files[int(selected)])

Comments

0

Create a dictionary where the key is an index and the value is the filename (use enumerate(dirList) to get a tuple of (index, filename) per iteration). Then just pull the corresponding value based on the input (key).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.