Do I need to pass the full path of a file in another directory to open()?
If you are just looking for the files in a single directory (ie you are not trying to traverse a directory tree, which it doesn't look like), why not simply use os.listdir():
import os for fn in os.listdir('.'): if os.path.isfile(fn): print (fn)
in place of os.walk(). You can specify a directory path as a parameter for os.listdir(). os.path.isfile() will determine if the given filename is for a file.
Yes, you need the full path.
log = open(os.path.join(root, f), 'r')
Is the quick fix. As the comment pointed out, os.walk
decends into subdirs so you do need to use the current directory root rather than indir
as the base for the path join.
You have to specify the path that you are working on:
source = '/home/test/py_test/'for root, dirs, filenames in os.walk(source): for f in filenames: print f fullpath = os.path.join(source, f) log = open(fullpath, 'r')