Python Forum
List of pathlib.Paths Not Ordered As Same List of Same String Filenames
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of pathlib.Paths Not Ordered As Same List of Same String Filenames
#1
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 supposed that the order retrieved by sorted() a list of Paths would be the same as the order retrieved by sorted() a list of their (string) filenames. But it is not the case.

Let me explain.

I have a list of filenames such as :

filenames_for_testing = (
    '/spam/spams.txt',
    '/spam/spam.txt',
    '/spam/another.txt',
    '/spam/binary.bin',
    '/spam/spams/spam.ttt',
    '/spam/spams/spam01.txt',
    '/spam/spams/spam02.txt',
    '/spam/spams/spam03.ppp',
    '/spam/spams/spam04.doc',
)
If I run the following:

sorted_filenames = sorted(filenames_for_testing)
print()
[print(element) for element in sorted_filenames]
print()
the alphabetical (string) order of this list will be:
  • /spam/another.txt
  • /spam/binary.bin
  • /spam/spam.txt
  • /spam/spams.txt
  • /spam/spams/spam.ttt
  • /spam/spams/spam01.txt
  • /spam/spams/spam02.txt
  • /spam/spams/spam03.ppp
  • /spam/spams/spam04.doc

But when I try to order the same list as pathlib.Paths using:

from pathlib import Path

paths_for_testing = [
    Path(filename)
    for filename in filenames_for_testing
]
sorted_paths = sorted(paths_for_testing)
The list returned is (just showing filenames of the pathlib.Paths):
  • /spam/another.txt
  • /spam/binary.bin
  • /spam/spam.txt
  • /spam/spams/spam.ttt
  • /spam/spams/spam01.txt
  • /spam/spams/spam02.txt
  • /spam/spams/spam03.ppp
  • /spam/spams/spam04.doc
  • /spam/spams.txt

which is different from previous list because 'spam/spams.txt' does not go after '/spam/spam.txt' and before all '/spam/spams/*' files (instead, it goes at the end of the list).

You can check it using:

sorted_filenames == [str(path) for path in sorted_paths]
which returns False.

I am not sure this would be a bug. Maybe it is the intended purpose. However, I think that it is a weird behaviour. Unless I am missing something, I can hardly understand why a list of pathlib.Paths and a list with the same string filenames can be ordered in the same fashion.

A crafted script to test this:
from pathlib import Path

# order string filenames

filenames_for_testing = (
    '/spam/spams.txt',
    '/spam/spam.txt',
    '/spam/another.txt',
    '/spam/binary.bin',
    '/spam/spams/spam.ttt',
    '/spam/spams/spam01.txt',
    '/spam/spams/spam02.txt',
    '/spam/spams/spam03.ppp',
    '/spam/spams/spam04.doc',
)

sorted_filenames = sorted(filenames_for_testing)

# output ordered list of string filenames

print()
print("Ordered list of string filenames:")
print()
[print(f'\t{element}') for element in sorted_filenames]
print()

# order paths (build from same string filenames)

paths_for_testing = [
    Path(filename)
    for filename in filenames_for_testing
]
sorted_paths = sorted(paths_for_testing)

# output ordered list of pathlib.Paths

print()
print("Ordered list of pathlib.Paths:")
print()
[print(f'\t{element}'
       ) for element in sorted_paths]
print()

# compare

print()

if sorted_filenames == [str(path) for path in sorted_paths]:
    print('Ordered lists of string filenames and pathlib.Paths are EQUAL.')
    
else:
    print('Ordered lists of string filenames and pathlib.Paths are DIFFERENT.')

    for element in range(0, len(sorted_filenames)):
        
        if sorted_filenames[element] != str(sorted_paths[element]):
            
            print()
            print('First different element:')
            print(f'\tElement #{element}')
            print(f'\t{sorted_filenames[element]} != {sorted_paths[element]}')
            break

print()
I am running Python 3.6.3 on MacOs 10.12.6

Thanks.
Reply


Messages In This Thread
List of pathlib.Paths Not Ordered As Same List of Same String Filenames - by QbLearningPython - Nov-11-2017, 05:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 3 402 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  Next/Prev file without loading all filenames WilliamKappler 9 790 Apr-12-2024, 05:13 AM
Last Post: Pedroski55
  Sample random, unique string pairs from a list without repetitions walterwhite 1 523 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  trouble reading string/module from excel as a list popular_dog 0 467 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,323 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  String to List question help James_Thomas 6 1,098 Sep-06-2023, 02:32 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,676 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 977 Feb-23-2023, 02:21 PM
Last Post: sparkt
  convert string to float in list jacklee26 6 2,018 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  Checking if a string contains all or any elements of a list k1llcod3 1 1,196 Jan-29-2023, 04:34 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020