This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.walk conflicts with os.listdir
Type: behavior Stage: resolved
Components: IO Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, shellster
Priority: normal Keywords:

Created on 2014-01-14 15:57 by shellster, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg208100 - (view) Author: Shelby Spencer (shellster) Date: 2014-01-14 15:57
The problem occurs when you attempt to perform an os.listdir on a directory that you don't have access to.  You should get an exception thrown.  Normally this occurs, however, if you run the following code:

folder = <some parent folder that has a child folder you don't have read access to>
for currentDir, subDirs, files = os.walk(folder):
    temp = os.listdir(currentDir)

temp will hold an empty list on the subdirectory you don't have access to and no exception is thrown.  This appears to be some sort of conflict between os.walk and os.listdir.  Near as I can tell python is caching the subdirectory listing for subDirs (os.walk doesn't throw an error on attempting to read a directory is doesn't have access to, obviously) and returning those results when os.listdir is called.
msg208101 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-01-14 16:11
That's not how Python works.  If you print out currentDir, you will see that os.listdir is never called on the folder for which you do not have permission.  That is, os.walk does indeed catch the listdir error, when *it* does the listdir, does not try to descend into that subdirectory.

If you want to do something with the error (such as raise it), use the 'onerror' argument of walk.
History
Date User Action Args
2022-04-11 14:57:56adminsetgithub: 64458
2014-01-14 16:11:13r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg208101

resolution: not a bug
stage: resolved
2014-01-14 15:57:58shellstercreate