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 generator giving inconsistent results
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Colin David Chen, eric.smith
Priority: normal Keywords:

Created on 2016-12-14 22:08 by Colin David Chen, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg283221 - (view) Author: Colin David Chen (Colin David Chen) Date: 2016-12-14 22:08
os.walk in 3.6rc1 (Windows) appears to generate different output depending on its invocation.

In the first invocation, I use the generator to yield one result at a time:
source = "C:\\cdchen_data\\downloads\\python-xlib-0.18\\"
texasranger = os.walk(Path(source))
roottree = [next(texasranger)]
roottree[0][0]
Output:
'C:\\cdchen_data\\downloads\\python-xlib-0.18'

The same result occurs when using the generator in a for loop.

In the second invocation, I generate the complete list first:
sourcetree = [x for x in os.walk(source)]
sourcetree[0][0]

Output:
'C:\\cdchen_data\\downloads\\python-xlib-0.18\\'

The particular behavior causing me trouble is the omission in the first result of the final '\\'. I checked in 2.7.6 and os.walk is consistent and I believe more correct in that it will yield equivalent results and includes the '\\'.

Not sure if earlier Python 3 implementations have this problem, I couldn't get 3.5 to run this function without failing.
msg283222 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2016-12-14 22:15
Isn't the difference that you use os.walk(Path(source)) in the first example, and os.walk(source) in the second one?
msg283223 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2016-12-14 22:19
Assuming Path is pathlib.Path, this is equivalent to the difference between:

>>> list(os.walk('/tmp'))
[('/tmp', <other-stuff ...>

and:
>>> list(os.walk('/tmp/'))
[('/tmp/', <other-stuff ...> 

Because:
>>> pathlib.Path('/tmp')
PosixPath('/tmp')
>>> pathlib.Path('/tmp/')
PosixPath('/tmp')
msg283224 - (view) Author: Colin David Chen (Colin David Chen) Date: 2016-12-14 22:32
Ah, you're correct. The behavior is consistent if you use strings in both invocations. The Path conversion strips the final '\\'. Doc examples do use the names of directories rather than a Path object.

I will close the issue. Thanks for the quick response!
History
Date User Action Args
2022-04-11 14:58:40adminsetgithub: 73161
2016-12-21 07:22:04eric.smithsetstage: resolved
2016-12-14 22:32:36Colin David Chensetstatus: open -> closed
resolution: not a bug
messages: + msg283224
2016-12-14 22:19:52eric.smithsetmessages: + msg283223
2016-12-14 22:15:26eric.smithsetnosy: + eric.smith
messages: + msg283222
2016-12-14 22:09:03Colin David Chensettitle: os.walk generator vs -> os.walk generator giving inconsistent results
2016-12-14 22:08:39Colin David Chencreate