python - os.walk() never returns when asked to print dirpaths -
i have simple directory structure:
rootdir\ subdir1\ file1.tif subdir2\ file2.tif ... subdir13\ file13.tif subdir14\ file14.tif
if call:
import os print os.listdir('absolute\path\to\rootdir')
...then you'd expect:
['subdir1', 'subdir2', ... 'subdir13', 'subdir14']
same thing happens if call os.listdir() on sub-directories. each 1 returns name of file in directory. no problems there.
and if call:
import os dirpath, dirnames, filenames in os.walk('absolute\path\to\rootdir'): print filenames print dirnames
...then you'd expect:
[] ['subdir1', 'subdir2', ... 'subdir13', 'subdir14'] ['file1.tif'] [] ['file2.tif'] [] ...
but here's strangeness. when call:
import os dirpath, dirnames, filenames in os.walk('absolute\path\to\rootdir'): print filenames print dirnames print dirpath
...it never returns, ever. if try:
print [each[0] each in os.walk('absolute\path\to\roodir')]
...or of sort. can print second , third parts of tuple returned os.walk(), moment try touch first part whole thing stops.
even stranger, behavior appears in scripts launched using shell. command line interpreter acts normally. i'm curious, what's going on here?
-----edit----- actual code:
allowed_imgformats = [".jpg",".tif"] def getcategorizedfiles(pathname): cats = [each[0] each in os.walk(pathname) if not each[0] == pathname] ncats = len(cats) tree = [[] in range(ncats+1)] cat in cats: catnum = int(os.path.basename(cat)) item in os.listdir(cat): if not item.endswith('.sift') , os.path.splitext(item)[-1].lower() in allowed_imgformats: tree[catnum].append(cat + '\\' + item) filedict = {cat : tree[cat] cat in range(1,ncats+1)} return filedict
----edit 2---- development. stated above, problem exists when code in scripts launched shell. not shell. problem exists console 2, not windows command prompt. exists when script launched java (how came across problem) so: http://www.programmersheaven.com/mb/python/415726/415726/invoking-python-script-from-java/?s=b20000
i've never trusted os.walk(). write own recursive stuff. it's not hard:
def contents(folder, l): # recursive, returns list of files full paths directcontents = os.listdir(folder) item in directcontents: if os.path.isfile(os.path.join(folder, item)): l.append(os.path.join(folder, item)) else:contents(os.path.join(folder, item), l) return l contents = contents(folder, [])
contents
list of files full paths included. can use os.split() if make little easier read.
knowing how works eliminates uncertainty of using os.walk() in code, means you'll able identify if problem in code involved os.walk().
if need put them in dictionary (because dictionaries have aliasing benefits, too), can sort files way.
Comments
Post a Comment