I am trying to get the order of all files in the directory so I tried using sorted() function to sort the numeric value of each file which are respectively named folder/frame0.jpg
. The problem is I am getting an output that looks like this
yourlieinapril1/frame1094.jpg
yourlieinapril1/frame1095.jpg
yourlieinapril1/frame1096.jpg
yourlieinapril1/frame1097.jpg
yourlieinapril1/frame1098.jpg
yourlieinapril1/frame1099.jpg
yourlieinapril1/frame1100.jpg
yourlieinapril1/frame110.jpg
yourlieinapril1/frame1101.jpg
when ever it iterates through a base ten number the list goes back to a lesser number which I tried fixing with the following code but it still doesn't work well
def animate(folder):
l = list()
a = list()
for filename in os.listdir(folder):
path = (os.path.join(folder, filename))
if path.endswith('jpg'):
num = ''
for i in range(len(path)):
try:
p = int(path[i])
num += str(path[i])
except:
pass
try:
check = a[len(a) - 1]
if len(check) > len(num):
if num != '0':
num += ('0')
path = folder + '/frame' + num + '.jpg'
except:
pass
a.append(num)
l.append(path)
f = sorted(l)
f = list(dict.fromkeys(f))
does anyone have an idea of how I should do this?