[New-bugs-announce] [issue20259] os.walk conflicts with os.listdir

Shelby Spencer report at bugs.python.org
Tue Jan 14 16:57:58 CET 2014

New submission from Shelby Spencer:

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.

components: IO
messages: 208100
nosy: shellster
priority: normal
severity: normal
status: open
title: os.walk conflicts with os.listdir
type: behavior
versions: Python 2.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list