0

I'm trying to print files present inside a folder in it's original order. Order inside folder [1.mp4 2.mp4 5.mp4 10.mp4 23.mp4]

Code I use:

targetFile = os.listdir(path)
for i in targetFile:
    print(i)

Output I get: 1.mp4 10.mp4 2.mp4 23.mp4 5.mp4

Output I expected: 1.mp4 2.mp4 5.mp4 10.mp4 23.mp4

How to fix it?

1 Answers1

0

The solution you seek is ordering the list as if they were an int. At the moment the result you are getting is based on how a string is sorted. If you can figure out how to change the key in sorting os.listdir(path), your problem is solved.

mikuszefski
  • 3,943
  • 1
  • 25
  • 38
Meenakshi Rai
  • 51
  • 1
  • 5
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/33138704) – buhtz Nov 09 '22 at 13:10
  • 1
    @buhtz true, it does not give the solution, but it shows the way how to solve it. For such a simple problem, I'd say that is appropriate. – mikuszefski Nov 10 '22 at 07:27