1

I need to open some files inside a folder in python

Say, I have the following files in the folder:

text_pbs.fna
text_pdom_fo_oo.fna
text_pdom_fo_oo_aa.fna
text_pdom_fo_oo.ali
text_pdom_ba_ar.fna
text_pdom_ba_ar_aa.fna
text_pdom_ba_ar.ali
text_pdom_ba_az.fna
text_pdom_ba_az_aa.fna
text_pdom_ba_az.ali

I want to open:

text_pdom_fo_oo.fna
text_pdom_ba_ar.fna
text_pdom_ba_az.fna

only.

I tried with glob:

glob.glob('*_pdom_*[^aa].fna')

But it doesn't work. Many thanks to point out the problem in the above pattern. Is there any other work around for this?

4
  • What exactly isn't working w/ glob.glob? Commented Jul 8, 2015 at 13:52
  • It's not taking any file. The above pattern is not matching anything. Commented Jul 8, 2015 at 13:56
  • Ok, on testing, it would appear that glob doesn't handle the ^ part of range matches. Commented Jul 8, 2015 at 14:00
  • Perhaps, you can do it with *_pdom_*[a-z][b-z].fna or *_pdom_*[b-z].fna. Commented Jul 8, 2015 at 14:08

1 Answer 1

5

The ^ is not handled and must be replaced by !, You should try this code:

import glob
glob.glob('*_pdom_*[!aa].fna')

gives the result:

['text_pdom_fo_oo.fna','text_pdom_ba_ar.fna','text_pdom_ba_az.fna']
Sign up to request clarification or add additional context in comments.

1 Comment

Might as well drop the second a in the brackets though since it doesn't do anything.

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.