-4

enter image description here

enter image description here

Hello, I have encountered a problem. When I use os.listdir, I hope that the effect of picture 1 will appear, but the effect of python is reversed. I would like to ask how can I get the data and want the effect of picture 1

  • 1
    Do not post images of text. Post them as text (plain or formatted). – Marcin Orlowski Nov 21 '22 at 10:14
  • 1
    Does this answer your question? [Non-alphanumeric list order from os.listdir()](https://stackoverflow.com/questions/4813061/non-alphanumeric-list-order-from-os-listdir) – kotatsuyaki Nov 21 '22 at 10:15

2 Answers2

0
import os
files = os.listdir()[::-1]
print(files)

somelist[::-1] reverses the list starts from the end towards the first taking each element as step=-1

See this answer

tovicheung
  • 132
  • 9
  • `os.listdir` returns a list of directories in an arbitrary order. This might work for OP on their current environment but can easily break for other environments. – Abdul Aziz Barkat Nov 21 '22 at 10:22
0

Do not post images of text (your second image). Use reverse()

files = os.listdir()
files.reverse()

It is unclear how the files are ordered in the screenshot given. This solution may not work in all environments, though this will solve the symptom of your problem as given.

Talpa
  • 66
  • 3