glob exclude pattern

Anastasios Andronidis picture Anastasios Andronidis · Dec 17, 2013 · Viewed 90.7k times · Source

I have a directory with a bunch of files inside: eee2314, asd3442 ... and eph.

I want to exclude all files that start with eph with the glob function.

How can I do it?

Answer

Kenly picture Kenly · Mar 29, 2016

The pattern rules for glob are not regular expressions. Instead, they follow standard Unix path expansion rules. There are only a few special characters: two different wild-cards, and character ranges are supported [from glob].

So you can exclude some files with patterns.
For example to exclude manifests files (files starting with _) with glob, you can use:

files = glob.glob('files_path/[!_]*')