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

C# random value from dictionary and tuple -

cgi - How do I interpret URLs without extension as files rather than missing directories in nginx? -

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