how to i get os.listdir with follow the file?

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

Upvotes: -4

Views: 207

Answers (2)

Talpa
Talpa

Reputation: 89

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.

Upvotes: 0

tovicheung
tovicheung

Reputation: 144

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

Upvotes: 0

Related Questions