0

I am creating a list with the files in a folder. The files are named like this: t1507859_Etappe-02-Alpe-Adria-Trail.svg. I want to split the strings to get something like: ["t1507859_Etappe-", "02", "-Alpe-Adria-Trail.svg"]. I want to get back the numbers on the second place of the list I got from the split operation.

dirs = os.listdir (path)

[i.split('-', 2)[1] for i in l]

print dirs

If i parse this code line by line into the python shell it works but not if I let it run as a module. There I just get the normal dir list.

Lukschn
  • 77
  • 4

1 Answers1

6

If i parse this code line by line into the python shell it works but not if I let it run as a module. There I just get the normal dir list.

Sure, this is because you are not assigning the result of the list comprehension to a variable. Instead, you meant:

dirs = os.listdir(path)
dirs = [i.split('-', 2)[1] for i in dirs]
print(dirs)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195