-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
  • 211
  • 1
  • 6

2 Answers2

4

Better use glob:

import glob
print glob.glob('/home/dir/*.ab')
perreal
  • 90,214
  • 20
  • 145
  • 172
3

no need regex :

[i for i in os.listdir('/home/dir/') if i.endswith(".ab")]
jrjc
  • 19,301
  • 8
  • 61
  • 75