I needed to write a little code that would grab the filenames of all zip files out of a directory hierarchy. I immediately tried to write it as a big list comprehension and completely failed. So, I backed off a step to the following (some code in this post refomatted to not break my layout):
finalfiles = []
for (dir, subdirs, files) in os.walk("c:/temp/"):
finalfiles.extend([
os.path.join(dir, file)
for file in files if file.lower().endswith(".zip")
])
I took a look at the list comprehension examples in the Python quick reference to figure out why I was having a problem. Here is their reference code (the two blocks are equivalent):
result = [expression for item1 in sequence1 if condition1
for item2 in sequence2 ... for itemN in sequenceN
]
result = []
for item1 in sequence1:
for item2 in sequence2:
...
for itemN in sequenceN:
if (condition1) and further conditions:
result.append(expression)
So, first I converted my finalfiles.extend() to an append() form:
finalfiles = []
for (dir, subdirs, files) in os.walk("c:/temp/"):
for file in files:
if file.lower().endswith(".zip"):
finalfiles.append(os.path.join(dir,file))
so that I could directly translate from the nested loop version to the list comprehension version, essentially doing textual substitution of item1, expression, and so on.
After doing the substitution, this is what you wind up with:
finalfiles = [
os.path.join(dir,file)
for (dir, subdirs, files) in os.walk("c:/temp/")
for file in files if file.lower().endswith(".zip")
]
That seems backwards to my brain, which is why I didn't get it right the first time. I think this is really a case where doing a list comprehension is not clearer than doing at least part of it in a normal loop fashion.
Note: my brain feels like it should be this way:
finalfiles = [
os.path.join(dir,file)
for file in files if file.lower().endswith(".zip")
for (dir, subdirs, files) in os.walk("c:/temp/")
]
which corresponds rather to:
result = [expression for item2 in sequence2
for item1 in sequence1
]
I think that makes more sense. Which fits your brain?
new⇒Court rejects death penalty for raping children - Yahoo! News
Keith is not a person. I have thison good authority. He's actually avery,...
M. Bean: Jul 4, 2:05am