-1

I have

os.listdir('/home/dir/')

with file and file.ab

How can I use regex to list only file.ab on that directory.

When i was use regex with

re.compile('*ab')

it return

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/re.py", line 190, in compile
    return _compile(pattern, flags)
  File "/usr/lib64/python2.6/re.py", line 245, in _compile
    raise error, v # invalid expression
sre_constants.error: nothing to repeat
haulpd
  • 257
  • 2
  • 6

2 Answers2

4

Better use glob:

import glob
print glob.glob('/home/dir/*.ab')
perreal
  • 94,503
  • 21
  • 155
  • 181
3

no need regex :

[i for i in os.listdir('/home/dir/') if i.endswith(".ab")]
jrjc
  • 21,103
  • 9
  • 64
  • 78