Skip to content

Instantly share code, notes, and snippets.

@saturngod
Created October 7, 2012 11:40
Show Gist options
  • Select an option

  • Save saturngod/3848050 to your computer and use it in GitHub Desktop.

Select an option

Save saturngod/3848050 to your computer and use it in GitHub Desktop.
File Sorting by Integer
import os
subDir = os.listdir(dirname)
#remove .DS_Store in mac osx
subDir = filter (lambda a: a != '.DS_Store', subDir)
#files name are Testing1.txt , Testing2.txt, Testing3.txt
toreplace = "Testing"
#x[0:len(toreplace)-1] is Testing
#int(x[len(toreplace):-4]) for 1, 2, 3,4,etc is integer value
#x[-4:] .txt
subDir = sorted(subDir, key=lambda x: (x[0:len(toreplace)-1],int(x[len(toreplace):-4]),x[-4:]))
for subname in subDir:
print subname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment