File Sorting in Python

I want to get sorted file array with python.

I have a array like following

  • Testing1.txt
  • Testing2.txt
  • Testing3.txt
  • Testing10.txt
  • Testing11.txt
  • Testing20.txt
  • Testing30.txt

When I use subDir = os.listdir(dirname) , it didn’t sort.

It will return like following order

  • Testing1.txt
  • Testin10.txt
  • Testin11.txt
  • Testing 2.txt
  • Testing 20.txt
  • Testing 3.txt
  • Testing 30.txt

We need to use sorted function to sort and we need to tell between Testing and .txt is a integer value.

Here is my code

[gist id=”3848050”]

comments powered byDisqus