You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Sorting pahtlib.Paths does give the same order as sorting the (string) filenames of that pathlib.Paths #76221
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2017-11-16.18:40:03.050>created_at=<Date2017-11-15.19:48:55.532>labels= ['extension-modules', 'type-bug', 'invalid']
title='Sorting pahtlib.Paths does give the same order as sorting the (string) filenames of that pathlib.Paths'updated_at=<Date2017-11-16.18:41:02.184>user='https://bugs.python.org/QbLearningPython'
While testing a module, I have found a weird behaviour of pathlib package. I have a list of pathlib.Paths and I sorted() it. I assumed that the order retrieved by sorting a list of Paths would be the same as the order retrieved by sorting the list of their corresponding (string) filenames. But it is not the case.
As you can see, 'spam/spams.txt' goes in different places if you have sorted by pathlib.Paths than if you have sorted by string filenames.
I think that it is weird that sorting pathlib.Paths yields a different result than sorting their string filenames. I think that pathlib.Paths should be ordered by alphabetical order of their corresponding filenames.
You have described the current behaviour of the pathlib package.
But let me ask: should be this the desired behaviour?
Since string filenames and pathlib.Paths are different ways to refer to the same object (a path in a filesystem), should not be they behaved in the same way when sorting?
You pointed out that the current behaviour is "more natural order" for pathlib.Paths. I am not truly sure about that. Can you please provide any citation or additional information about that?
It is "obvious by inspection". Paths are paths instead of strings because they are formed out of discrete path components instead of strings. If you sorted each directory in the paths from the top down, and then sorted the subdirectories, and then sorted the filenames, you get that sorting by component. It's the same order you would get out of an ls -R.
Activity
QbLearningPython commentedon Nov 15, 2017
While testing a module, I have found a weird behaviour of pathlib package. I have a list of pathlib.Paths and I sorted() it. I assumed that the order retrieved by sorting a list of Paths would be the same as the order retrieved by sorting the list of their corresponding (string) filenames. But it is not the case.
I run the following example:
==========================================================================
# order string filenames
# output ordered list of string filenames
# order paths (build from same string filenames)
# outoput ordered list of pathlib.Paths
# compare
else:
print('Ordered lists of string filenames and pathlib.Paths are DIFFERENT.')
==========================================================================
The output of this script was:
==========================================================================
Ordered list of string filenames:
Ordered list of pathlib.Paths:
Ordered lists of string filenames and pathlib.Paths are DIFFERENT.
First different element:
Element #3
/spam/spams.txt != /spam/spams/spam.ttt
==========================================================================
As you can see, 'spam/spams.txt' goes in different places if you have sorted by pathlib.Paths than if you have sorted by string filenames.
I think that it is weird that sorting pathlib.Paths yields a different result than sorting their string filenames. I think that pathlib.Paths should be ordered by alphabetical order of their corresponding filenames.
Thank you.
serhiy-storchaka commentedon Nov 15, 2017
Paths are ordered by lexicographical order of their corresponding components. Paths are not strings, and this this order is more natural for them.
The alphabetical order of Path strings:
The lexicographical order of Path components:
QbLearningPython commentedon Nov 16, 2017
Thanks, serhiy.storchaka, for your answer.
I am not fully convinced.
You have described the current behaviour of the pathlib package.
But let me ask: should be this the desired behaviour?
Since string filenames and pathlib.Paths are different ways to refer to the same object (a path in a filesystem), should not be they behaved in the same way when sorting?
You pointed out that the current behaviour is "more natural order" for pathlib.Paths. I am not truly sure about that. Can you please provide any citation or additional information about that?
Thank you.
bitdancer commentedon Nov 16, 2017
It is "obvious by inspection". Paths are paths instead of strings because they are formed out of discrete path components instead of strings. If you sorted each directory in the paths from the top down, and then sorted the subdirectories, and then sorted the filenames, you get that sorting by component. It's the same order you would get out of an ls -R.
bitdancer commentedon Nov 16, 2017
To put it another way, think about sorting a list of tuples. Same behavior.