Function similar to os.listdir(), but i can see step by step adding it to list

30 Views Asked by At

I am looking for a function or a way, something similar to os.listdir(), however, I need to add the dir various times into the list depending on the path. For example:

os.listdir(path)

Output:

['Bacon.png', 'Basics.png', 'Cabbage.png', 'Elf.png', 'Flip Flop.png', 'Prisoner.png', 'Santa Blue.png']

I need something like this:

['Bacon','Bacon','Bacon','Bacon', 'Basics', 'Basics', 'Cabbage', 'Elf', 'Elf', 'Elf', 'Flip Flop', 'Prisoner', 'Prisoner', 'Santa Blue']

Basically, I need to multiply the number of times listdir imports the path or item into the list. Thank you for any help and interactions.

1

There are 1 best solutions below

0
Barmar On
result = []
for filename in os.listdir(path):
    result.extend([os.path.splitext(filename)[0]] * repetitions(path, filename))

repetitions() is a function you can write that determines the number of times the filename should be multiplied.