How can I sort jpgs in a folder to 1,2,3,4 and so on?
Where do I have a mistake in my code?
import os
path = "/content/drive/My Drive/output_last"
fds = sorted(os.listdir(path))
print(fds)
How can I sort jpgs in a folder to 1,2,3,4 and so on?
Where do I have a mistake in my code?
import os
path = "/content/drive/My Drive/output_last"
fds = sorted(os.listdir(path))
print(fds)
It sounds like you want to sort numerically:
import os
path = "/content/drive/My Drive/output_last"
fds = sorted(os.listdir(path), key=lambda x: int(x))
print(fds)
If your filenames have the .jpg
extension, you will need sort on just the base filename. Something like this:
import os
path = "/content/drive/My Drive/output_last"
fds = sorted(os.listdir(path), key=lambda x: int(x.split('.jpg')[0]))
print(fds)