python - Create dynamic menu from os.listdir -
how generate list of dynamic menu items based on number of files present in folder?
the current code retrieves filenames in folder, needs generate menu options.
from os import listdir os.path import isfile, join folder = "path/folder/to/read/" file_names = [fn fn in listdir(folder) if isfile(join(folder,fn))] print "select file manipulate:\n" f in file_names: print #add iterable menu items here somehow
desired functionality:
"select file manipulate: [1] test.csv [2] test2.csv [3] test3.csv"
it should take raw_input
1
, 2
or 3
, select corresponding f
in file_names
. folder
+ ans
create full path path/folder/to/read/test.csv
.
static example:
while ans: print (""" [1]. option 1 [2]. option 2 [3]. option 3 """) ans = raw_input("select action: ") if ans == "1": #do if ans == "2": #do else if ans == "3": #do different
made own solution:
folder = "path/folder/to/read/" file_names = [fn fn in listdir(folder) if isfile(join(folder,fn))] count = -1 f in file_names: count = count + 1 print "[%s] " % count + f while true: ans_file = input("select file: ") if ans_file > count: print "wrong selection." continue path = folder + file_names[ans_file] print "selected file: %s " % path break
Comments
Post a Comment