i searching how absolute path of file on python haven't had luck. here code
import os directory = raw_input("what's directory? ") print "reading files.." listing = os.listdir(directory) fname in listing: open(fname) f: if "yes" in f.read(): print f.name f.close()
my problem this... listing fine.. when listdir method passes variable open method, variable passes without absolute path, won't read files because reading file has no path.. here sample of error
what's directory? /home/diego/test reading files.. traceback (most recent call last): file "/home/diego/documents/progra/python/antivirus/antivirus.py", line 14, in open(fname) f: ioerror: [errno 2] no such file or directory: 'test'
can me it?
thanks
ok, problem hasn't got absolute paths. problem you're trying open file exists in directory using it's filename. use os.path.join
create complete file name , use that:
for fname in listing: full_name = os.path.join(directory, fname) open(full_name) f: if "yes" in f.read(): print f.name