Note that there are some explanatory texts on larger screens.

plurals
  1. POos.walk() never returns when asked to print dirpaths
    primarykey
    data
    text
    <p>I have a simple directory structure:</p> <pre><code>rootdir\ subdir1\ file1.tif subdir2\ file2.tif ... subdir13\ file13.tif subdir14\ file14.tif </code></pre> <p>If I call:</p> <pre><code>import os print os.listdir('absolute\path\to\rootdir') </code></pre> <p>...then I get what you'd expect:</p> <pre><code>['subdir1', 'subdir2', ... 'subdir13', 'subdir14'] </code></pre> <p>Same thing happens if I call os.listdir() on those sub-directories. For each one it returns the name of the file in that directory. No problems there. </p> <p>And if I call:</p> <pre><code>import os for dirpath, dirnames, filenames in os.walk('absolute\path\to\rootdir'): print filenames print dirnames </code></pre> <p>...then I get what you'd expect:</p> <pre><code>[] ['subdir1', 'subdir2', ... 'subdir13', 'subdir14'] ['file1.tif'] [] ['file2.tif'] [] ... </code></pre> <p>But here's the strangeness. When I call:</p> <pre><code>import os for dirpath, dirnames, filenames in os.walk('absolute\path\to\rootdir'): print filenames print dirnames print dirpath </code></pre> <p>...it never returns, ever. Even if I try:</p> <pre><code>print [each[0] for each in os.walk('absolute\path\to\roodir')] </code></pre> <p>...or anything of the sort. I can always print the second and third parts of the tuple returned by os.walk(), but the moment that I try to touch the first part the whole thing just stops. </p> <p>Even stranger, this behavior only appears in scripts launched using the shell. The command line interpreter acts normally. I'm curious, what's going on here? </p> <p>-----EDIT----- Actual code:</p> <pre><code>ALLOWED_IMGFORMATS = [".jpg",".tif"] def getCategorizedFiles(pathname): cats = [each[0] for each in os.walk(pathname) if not each[0] == pathname] ncats = len(cats) tree = [[] for i in range(ncats+1)] for cat in cats: catnum = int(os.path.basename(cat)) for item in os.listdir(cat): if not item.endswith('.sift') and os.path.splitext(item)[-1].lower() in ALLOWED_IMGFORMATS: tree[catnum].append(cat + '\\' + item) fileDict = {cat : tree[cat] for cat in range(1,ncats+1)} return fileDict </code></pre> <p>----EDIT 2---- Another development. As stated above, this problem exists when the code is in scripts launched from the shell. But not any shell. The problem exists with Console 2, but not the Windows command prompt. It also exists when the script is launched from java (how I originally came across the problem) like so: <a href="http://www.programmersheaven.com/mb/python/415726/415726/invoking-python-script-from-java/?S=B20000" rel="nofollow">http://www.programmersheaven.com/mb/python/415726/415726/invoking-python-script-from-java/?S=B20000</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload