Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression usage in glob.glob?

Tags:

python

glob

import glob  list = glob.glob(r'*abc*.txt') + glob.glob(r'*123*.txt') + glob.glob(r'*a1b*.txt')  for i in list:   print i 

This code works to list files in the current folder which have 'abc', '123' or 'a1b' in their names.

How would I use one glob to perform this function?

like image 922
user1561868 Avatar asked Oct 23 '12 13:10

user1561868


People also ask

Does glob take regex?

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.

What does glob glob return?

The glob. glob returns the list of files with their full path (unlike os. listdir()) and is more powerful than os. listdir that does not use wildcards.

Is glob a standard library?

The glob Module - Python Standard Library [Book]


2 Answers

The easiest way would be to filter the glob results yourself. Here is how to do it using a simple loop comprehension:

import glob res = [f for f in glob.glob("*.txt") if "abc" in f or "123" in f or "a1b" in f] for f in res:     print f 

You could also use a regexp and no glob:

import os import re res = [f for f in os.listdir(path) if re.search(r'(abc|123|a1b).*\.txt$', f)] for f in res:     print f 

(By the way, naming a variable list is a bad idea since list is a Python type...)

like image 51
Schnouki Avatar answered Oct 10 '22 00:10

Schnouki


I'm surprised that no answers here used filter.

import os import re  def glob_re(pattern, strings):     return filter(re.compile(pattern).match, strings)  filenames = glob_re(r'.*(abc|123|a1b).*\.txt', os.listdir()) 

This accepts any iterator that returns strings, including lists, tuples, dicts(if all keys are strings), etc. If you want to support partial matches, you could change .match to .search. Please note that this obviously returns a generator, so if you want to use the results without iterating over them, you could convert the result to a list yourself, or wrap the return statement with list(...).

like image 35
Evan Avatar answered Oct 10 '22 00:10

Evan