1

I have a directory with 1600 photos and I need to save the path to each foto to a list and then to .txt file.

The photos are enumerated according to the position they should have in the list: img(0), img(1)... and I need this position to be kept.

What I obtain is this order, so now in list index 2 I have img(10):

img(0) img(1) img(10) img(100) img(1000) img(1001)...
img(2) img(2) img(20) img(200) img(2000) img(2001)...

Apparently, I'm the only one having this issue because I didn't find any discussion about this problem. Thank you very much for helping me.

Furin
  • 532
  • 10
  • 31
  • 1
    Is it not getting every image file or is it just getting them in a different order than you expected? – Sam Craig Feb 21 '18 at 16:42
  • It *is* scanning sequentially; it is just doing so in lexicographic order which encompasses all characters, rather than doing so in numerical order for the numeric portion of the list. – chepner Feb 21 '18 at 16:43
  • 1
    The documentation of `os.scandir` says that it returns the files in arbitrary order, so you need to apply your desired sorting. You are probably looking for the so called "natural sorting". – pschill Feb 21 '18 at 16:43
  • 2
    @chepner The documentation says it scans in arbitrary order so I don't think that is the case. – Sam Craig Feb 21 '18 at 16:44
  • 1
    It is scanning sequentially, I think "arbitrary" means that the user doesn't know the ordering relation (could be the creation time? the *inode id*?). I expect that if no change occurs in a directory structure `os.scandir` to yield the same result every time it's called. – CristiFati Feb 21 '18 at 16:55

1 Answers1

0

As mentioned by others, the documentation does not guarantee any particular ordering. In your case it appears to be sorted alphabetically/lexicographically. "10" comes before "2" alphabetically. You'll have to prepend 0s to give every file the same number of digits to get the ordering you want if this behaviour appears to remain consistent on your machine.

For example, "002" will come before "010".

If you want to be safe (for example need to be able to port your code to other machines/OSes), you'll want to manually sort.

Dennis Soemers
  • 8,090
  • 2
  • 32
  • 55
  • 4
    https://docs.python.org/3/library/os.html#os.scandir According to the documentation, it scans in arbitrary order, not lexicographic. OP should resort after scanning. – Sam Craig Feb 21 '18 at 16:45
  • @SamCraig you're right, but that's a duplicate – Jean-François Fabre Feb 21 '18 at 16:48
  • @SamCraig in practice it does seem not to be arbitrary for the OP though, and I suspect my original answer was useful for his specific case. You're right though, updated my answer to be more correct – Dennis Soemers Feb 21 '18 at 16:53
  • Oh, I see, I think @DennisSoemers solution and the natural sort one (thanks @Jean-FrançoisFabre) can be fine. Thank you very much! – Furin Feb 21 '18 at 17:23
  • Just as update: natsorted (https://pypi.python.org/pypi/natsort) solved my problems. Thanks again everyone. – Furin Feb 22 '18 at 12:02