Monday, 15 March 2021

How to sort file name according to numerical order in python?

 

import numpy as np
import glob
from natsort import natsorted

for file in natsorted(glob.glob("before_255/*")):
print(file)
 
---
 
before_255/0.npy
before_255/1.npy
before_255/2.npy
before_255/3.npy
before_255/4.npy
before_255/5.npy
before_255/6.npy
before_255/7.npy
before_255/8.npy
before_255/9.npy
before_255/10.npy
before_255/11.npy
before_255/12.npy
before_255/13.npy
before_255/14.npy
before_255/15.npy
before_255/16.npy
before_255/17.npy
before_255/18.npy
before_255/19.npy
before_255/20.npy 

No comments:

Post a Comment