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

Time:11-21

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related