I have the below files in a directory:
Using os.listdir()
I am reading all the files and then saving them in a list. Below is the code:
y = []
files = os.listdir()
for file in files:
if "mb" in file:
file = file.split("-")
loss = file[5]
lossNum = loss.split('.pth')
y.append(round(float(lossNum[0]), 3))
print(y)
In above code, I am reading the file name and then splitting it so that I get the number for ex 8.199
or 6.184
and I am saving them in the list. Below is the output of the list:
[8.2, 6.185, 4.115, 4.425, 3.897, 3.972, 5.672, 6.112, 6.129, 5.382, 4.558, 5.476, 4.526, 4.579]
Values in the above list is not as per the filenames. For ex, value at index 0 and 1 are correct because in file name Epoch-0
and Epoch-1
has the same number but Epoch-2
has number 5.67
but index 2 of list contains 4.11
which is wrong. This is happening because when we do os.listdit()
it is automatically list Epoch-0, Epoch-1, and then Epoch-10, Epoch-11, Epoch-12 instead of Epoch-2, Epoch-3 and so on. How can I correct this issue?
Files:
["mb1-ssd-Epoch-0-Loss-8.199731510297386.pth",
"mb1-ssd-Epoch-1-Loss-6.184953727553376.pth",
"mb1-ssd-Epoch-10-Loss-4.114924973091193.pth",
"mb1-ssd-Epoch-11-Loss-4.4250144663110245.pth",
"mb1-ssd-Epoch-12-Loss-3.896865705473233.pth",
"mb1-ssd-Epoch-13-Loss-3.972265353245018.pth.filepart",
"mb1-ssd-Epoch-2-Loss-5.671893659946138.pth",
"mb1-ssd-Epoch-3-Loss-6.111974941945709.pth",
"mb1-ssd-Epoch-4-Loss-6.128832694703498.pth",
"mb1-ssd-Epoch-5-Loss-5.382261596949754.pth",
"mb1-ssd-Epoch-6-Loss-4.558234235881704.pth",
"mb1-ssd-Epoch-7-Loss-5.47572956253997.pth",
"mb1-ssd-Epoch-8-Loss-4.526285114541518.pth",
"mb1-ssd-Epoch-9-Loss-4.578502741535153.pth"]
Do this to sort your files in the order shown in image uploaded in your question:
files = os.listdir()
files.sort(key=lambda x: int(x.split("-")[3]))