huda
huda

Reputation: 37

os.listdir command is not picking files in the correct storing order

I have stored files in one directory having order J_0.44105, J_0.44107, J_0.44109, J_0.44111. But when I load files by following code it doesn't pick files in correct order.

`df=os.listdir("folder")
for df in datafiles:
    print df`

Upvotes: 0

Views: 26

Answers (1)

blhsing
blhsing

Reputation: 106713

os.listdir returns the file names in the order the files are stored on the file system, which can appear to be unordered. You should simply sort the list on your own with the sorted function:

for df in sorted(os.listdir("folder")):
    print df

Upvotes: 1

Related Questions