Making Python display .txt files from directories -


print "which category view? savory, dessert, cake, soup or drink?    " category = raw_input() if category == "savory": #opens directory according answer.     os.listdir("savory")  elif category == "drink":     os.listdir("drink")  elif category == "cake":     os.listdir("cake")  elif category == "dessert":     os.listdir("dessert")  elif category == "savory":     os.listdir("savory")  elif category == "drink":     os.listdir("drink")  elif category == "cake":     os.listdir("cake")  elif category == "dessert":     os.listdir("dessert") 

i trying create code display recipes have been saved under directories. when run code nothing displayed if input recipes directories.

firstly, you're not printing anything. e.g

os.listdir("savory") 

should

print(os.listdir("savory")) 

secondly, can dramatically shorten code:

print("which category view? savory, dessert, cake, soup or drink?") print(os.listdir(raw_input().lower())) 

if want restrict user accessing other directories:

safe = ['savory', 'dessert', 'cake', 'soup', 'drink']  category = raw_input().lower() if category in safe:     print(os.listdir(category)) else:     print('you have typed invalid category') 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -