python - os.path.join does not yield the contents of the file -


print "which category view? savory, dessert, cake, soup or drink?    " category = raw_input() x in os.listdir(category): print x name = raw_input("which recipe wou view?   ") fullname = os.path.join(category, name) f = open(fullname, "r"); print f 

i writing program allow users view contents of .txt files saved in specific directories. when run code don't contents , instead message says this:
open file 'savory/b.txt', mode 'r' @ 0x1004bd140

any ideas. new python dont have of idea causing error assume due missing code. thank you.

the return value of open file object (not file contents!) .you need call method on file object read file:

f = open(fullname, "r") print f.read() f.close() 

if it's big file may want iterate on file line-by-line

f = open(fullname, "r") line in f:   print line f.close() 

on side note, here's alternate syntax don't have remember call close method:

with open(fullname, "r") f:   line in f:     print line 

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 -