1

So I have a script where I loops through a set of files in a folder. After retrieving the list of files in that particular directory, how do I specify which files I want to use in the script?

target = './directory/'

for file in listdir(target):

Now I have several different files in the same folder.

  • kplr006933899-2009131105131_llc.fits
  • kplr006933899-2009131105131_lpd-targ.fits
  • kplr006933899-2012151031540_slc.fits
  • kplr006933899-2012151031540_spd-targ.fits

They all are part of the same group, which is denoted by "kplr006933899". How can I specify parts of the strings as different variable in order to specify which files I want to loop through?

Like for example:

def function(name,types)

where you could write when called:

function(kplr006933899,[slc,llc])
1
  • 1
    Could you explain better? I mean, you want to group together all the files that start with kplr + some number and that contain specific other substrings or you simply want to filter the names that start with kplr and contain some substring? Commented Feb 3, 2013 at 20:19

2 Answers 2

3

There are multiple ways of doing this. First way:

import fnmatch

def my_function(name, types):
    result = []
    for t in types:
        pattern = "{}*{}.fits".format(name, t)
        for filename in fnmatch.filter(listdir(target), pattern):
            result.append(filename)
    return result

You can call this function with: my_function("kplr006933899", ["slc", "llc"]). The fnmatch.filter function performs the pattern matching with your pattern and the given filenames.

The second way is to use glob:

result = []
for t in types:
    result.extend(glob.glob("{}/{}*{}.fits".format(target, name, t)))
return result
Sign up to request clarification or add additional context in comments.

2 Comments

@Bakuriu: Thanks, makes sense. I've added the glob solution as well.
Thank you simeon! This is perfect!
0
>>> "kplr" in  "kplr006933899-2009131105131_llc.fits"
True
>>> 
>>> "kplR" in  "kplr006933899-2009131105131_llc.fits"
False
>>> 

Notice that you need to put quotes to denote a string function("kplr006933899", [slc, llc]), otherwise kplr006933899 will be interpreted as a variable.

1 Comment

Oh yeah, that was just a typo. I was just looking into the fnmatch function, and I got this far: 'for file in os.listdir('./'): if fnmatch.fnmatch(file,'kplr006933899*slc.fits'):' Now I want to be able to specify the beginning of the string, and maybe a list of possible ends for the string, like slc, llc, so then it would only loop through the files with slc or llc at the end of the filename.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.