-3

How can i sort files in a folder for example user/images/ which contains files in this order list below, objective is not to print a list of file names which is sorted but to actually rearrange files by sorting in the above mentioned folder

Current order

1.jpg
5.jpg
3.jpg
4.jpg
2.jpg

Desired order

1.jpg
2.jpg
3.jpg
4.jpg
5.jpg
J33T
  • 13
  • 6
  • What OS and file system are you working with, and how are you retrieving the list of file names? – Mathias R. Jessen Sep 13 '22 at 13:20
  • 5
    Files in a directory, by themselves, have no concept of order. It is all up to the tool that is displaying those files to the user to choose the order in which to display them. – 0x5453 Sep 13 '22 at 13:21
  • @MathiasR.Jessen i am using google colab – J33T Sep 13 '22 at 13:28
  • 1
    @J33T looks like [colab might preserve insertion order](https://stackoverflow.com/questions/66537490/image-data-is-being-stored-in-different-random-order-in-array-after-reading-from), you'll simply have to sort them as desired when enumerating them. – Mathias R. Jessen Sep 13 '22 at 13:35

1 Answers1

5

Directories generally don't have an intrinsic sort order for files (or if they do, that's entirely filesystem implementation specific; as an anecdote, I had to once write a program that would copy files onto an external drive in reverse order, so a cheap MP3 player would list them in the correct order).

That's why there is no simple, generic way to "actually rearrange files" in a folder.

AKX
  • 152,115
  • 15
  • 115
  • 172