Reputation: 1416
I've done an OS walk and appended the results to a list.
exportFolder = []
for files in os.walk(exportLocation):
exportFolder.append(files)
This is the list that is being stored as exportFolder:
[('/home/simon/Desktop/Windows.edb.export', [], ['MSysUnicodeFixupVer2.2', 'SystemIndex_0P.6', 'SystemIndex_0A.7', 'SystemIndex_GthrPth.5', 'SystemIndex_Gthr.4', 'MSysObjects.0', '__NameTable__.3', 'MSysObjectsShadow.1'])]
I want to search the list for the file 'SystemIndex_0A'
so I use:
filter(lambda x: 'SystemIndex_0A' in x, exportFolder)
The results bring back an empty list: []
I can see that the exportFolder list seems to be a list inside a list. How can I get my filter to work?
I want to print the SystemIndex_OA file from the list
Thanks in advance.
Upvotes: 0
Views: 831
Reputation: 103874
Maybe rewrite your walk
loop to look something more like this:
tgt=[]
for root, dirs, files in os.walk(exportLocation):
for file in files:
if 'SystemIndex_0A' in file:
tgt.append(os.path.join(root, file))
Then you don't need to filter a nested list of tuples.
If you want to use a list of tuples produces by os.walk
, you would do something like this (using your example, so we will only use the first tuple):
>>> LoT=[('/home/simon/Desktop/Windows.edb.export', [], ['MSysUnicodeFixupVer2.2', 'SystemIndex_0P.6', 'SystemIndex_0A.7', 'SystemIndex_GthrPth.5', 'SystemIndex_Gthr.4', 'MSysObjects.0', '__NameTable__.3', 'MSysObjectsShadow.1'])]
>>>
>>> path, dirs, files=LoT[0]
>>> path
'/home/simon/Desktop/Windows.edb.export'
>>> dirs
[]
>>> files
['MSysUnicodeFixupVer2.2', 'SystemIndex_0P.6', 'SystemIndex_0A.7', 'SystemIndex_GthrPth.5', 'SystemIndex_Gthr.4', 'MSysObjects.0', '__NameTable__.3', 'MSysObjectsShadow.1']
Now that you have used tuple unpacking to unpack the tuple, you can use your filter statement:
>>> filter(lambda x: 'SystemIndex_0A' in x, files)
['SystemIndex_0A.7']
So if you had a whole list of tuples, you would use two nested loops.
Upvotes: 1