can we sort subfolders in a folder and then every subfolder has a png file such as
['1.2.392.200036.9116.4.2.7383.1467.20210811020536539.1.2', '1.2.392.200036.9116.4.2.7383.9500.20210811020536560.1.4', '1.2.392.200036.9116.4.2.7383.7724.20210811020536578.1.6', '1.2.392.200036.9116.4.2.7383.9962.20210811020536605.1.9', '1.2.392.200036.9116.4.2.7383.7334.20210811020536551.2.3', '1.2.392.200036.9116.4.2.7383.2169.20210811020536569.2.5', '1.2.392.200036.9116.4.2.7383.3478.20210811020536587.2.10', '1.2.392.200036.9116.4.2.7383.3358.20210811020536596.9.11',
I want to sort folder with last 2 digits and then every folder has a png file inside it, so i want to access it in assending orde
CodePudding user response:
Try something like:
from natsort import natsorted #py -m pip install natsort
import os
files = []
folders = ['1.2.392.200036.9116.4.2.7383.1467.20210811020536539.1.2', '1.2.392.200036.9116.4.2.7383.9500.20210811020536560.1.4', '1.2.392.200036.9116.4.2.7383.7724.20210811020536578.1.6', '1.2.392.200036.9116.4.2.7383.9962.20210811020536605.1.9', '1.2.392.200036.9116.4.2.7383.7334.20210811020536551.2.3', '1.2.392.200036.9116.4.2.7383.2169.20210811020536569.2.5', '1.2.392.200036.9116.4.2.7383.3478.20210811020536587.2.10', '1.2.392.200036.9116.4.2.7383.3358.20210811020536596.9.11']
sort(folders,key=lambda x: int(x.split('.')[-1]))
for folder in folders:
subfolder_files = list(os.listdir(folder))
subfolder_files = natsorted(subfolder_files)
files.extend(subfolder_files)
print(files)