I have a list of files named like:
file1.jpg
file2.jpg
.
.
.
where occasionally there is a duplicate such that the correct order should be:
fileN.jpg
fileN 1.jpg
fileN 2.jpg
not:
fileN 1.jpg
fileN 2.jpg
fileN.jpg
Note in my use case, the string fileN
can contain special characters (including spaces). With the following python script, I get almost what I want, but with the incorrect ordering for the special duplicate case (i.e, the latter example above instead of the one before it).
import os;
files = os.listdir(os.path.curdir)
files.sort()
for file in files:
print(file)
How do I get the correct ordering, as shown in my 2nd code snippet? (I’m sure there is a more elegant CS name for the sorting I am looking for – that terminology would also be appreciated.)