Reputation: 67
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.
Upvotes: 0
Views: 205
Reputation: 473763
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)
Upvotes: 6