tags:

views:

78

answers:

1

I have a list of directories (their absolute path). Each directory contains a certain number of files. Of these files I want to get two of them from each directory. The two files I want have some string pattern in their name, for the sake of this example the strings will be 'stringA', 'stringB'.

So what I need is a list of tuples. Each tuple should have a stringA file and a stringB file in it. There should be one tuple per directory. Each directory is guaranteed to have more than 2 files and is guaranteed to have only one stringA and one stringB file.

What is the most efficient way to do this? Maybe using a list generator?

Edit:

An example:

dirs = ['/dir1', '/dir2', '/dir3']

result = [('/dir1/stringA.txt', '/dir1/stringB.txt'), ('/dir2/stringA.txt', ...) ...]

The input is directories (a list of directories) and the output should be the result (a list of tuples).

+1  A: 

See if this works for you:

import glob
result = zip(sorted(glob.glob('/dir/*stringA*')), sorted(glob.glob('/dir/*stringB*')))
Gintautas Miliauskas
+1 twas my answer too
awesomo
is there a way to generalize the '/dir/*stringA*' to work for any directory? Edit: Actually I don't get how this works. How does it iterate over the list of dictionaries?
Robert Marcado
Sure there is a way to generalize to any directory. Just construct the string manually by passing, e.g., '/' + mydir + '/*stringA*'. Also, there's no iteration over dictionaries: glob() returns a list of strings, and zip() "zips" the two lists of strings up into a list of tuples (like a zipper).
Gintautas Miliauskas
Woops I did not mean iterating over a list of dictionaries. I meant given the input list dirs. How does it iterate over that? Or were you just giving a general example.
Robert Marcado