1

I wrote this script that will show all files within a specific directory and will let the user input which file he wants to edit.

import os

path = "/home/luai/Desktop/python/test"
dirs = os.listdir( path )

print "Here is a list of all files: "

for files in dirs:
    print files

filename = raw_input ("which file to update: ")
if filename in dirs:
    inputFile = open(filename, 'r')
else:
    print "no match found"
    sys.exit()
    inputFile.close()

and its working, but the thing is that I want the user to only enter a number or a letter to open the file instead of writing the whole name of the file. any ideas ?

thanks.

4
  • 2
    Didn't you basically answer your own question in the title? Commented Nov 24, 2015 at 12:58
  • @luk32 yes but its not working out, there must be something im doing wrong but I have not figure what is it yet Commented Nov 24, 2015 at 13:04
  • You are not even trying to use dict yet. Try it 1st then present an actual problem. "Any ideas?" is not a very good question. Especially since you missed any clear problem statement. Commented Nov 24, 2015 at 13:07
  • I did try :) If you're not trying to help better don't comment. @luk32 Commented Nov 24, 2015 at 13:11

3 Answers 3

1

I would just loop over the listdir output with an index:

import os

path = "/home/luai/Desktop/python/test"
dirs = os.listdir( path )

print "Here is a list of all files: "

for filenumber in range(len(dirs)):
    print filenumber, dirs[filenumber]

filenumber = raw_input ("Number of file which to update: ")
filenumber = int(filenumber)

if dirs[filenumber] in dirs:
    inputFile = open(os.path.join(path, dirs[filenumber]))
    inputFile.close() #this line was at a weird place?
else:
    print "no match found"
    sys.exit()

But this still has the problem that listdir also gives you directories, and you can't open those. So best is to only get the files from path.

import os

path = "/home/luai/Desktop/python/test"
files = os.walk(path).next()[2]


print "Here is a list of all files: "

for filenumber in range(len(files)):
    print filenumber, files[filenumber]

filenumber = raw_input ("Number of file which to update: ")
filenumber = int(filenumber)

if files[filenumber] in files:
    inputFile = open(os.path.join(path, files[filenumber]))
    inputFile.close()
else:
    print "no match found"
    sys.exit()
Sign up to request clarification or add additional context in comments.

Comments

1

You can create a dictionary with values as the files' names and the keys as whatever you want it to be. Based on the user input, you check if the key exists, and open the corresponding file name value.

Comments

0

Ruben has the right idea, but it can be a bit more elegant:

chosen_file_index = -1  # Intentionally invalid index

while chosen_file_index not in range(0, len(filenames)-1):

    filenames = os.listdir(path)

    if not filenames:
          print "No files found"
          break

    for file_index, filename in enumerate(filenames):
        print "{index}. {filename}".format(index=file_index, filename=filename)

    chosen_file_index = raw_input("Which file to update:")

chosen_filename = filenames[chosen_file_index)

So here I've added the idea that the system will keep asking until the user chooses a valid entry from the list and also handled the case where there are no files.

2 Comments

I like it, but it still has directories, so that'll cause error later on
Yeah, I saw you added that to your answer, but he didn't actually ask about that so I didn't want to add too many things to the answer. Good catch.

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.