Steven D'Aprano wrote:
On Mon, 22 Feb 2010 04:23:04 am Dayo Adewunmi wrote:
Hi all

I'm trying use regex to match image formats:

Perhaps you should use a simpler way.

def isimagefile(filename):
    ext = os.path.splitext(filename)[1]
return (ext.lower() in ('.jpg', '.jpeg', '.gif', '.png', '.tif', '.tiff'))


def findImageFiles():
    someFiles = [
    "sdfinsf.png","dsiasd.dgf","wecn.GIF","iewijiefi.jPg","iasjasd.py"]
    return filter(isimagefile, someFiles)


$ python test.py
Traceback (most recent call last):
  File "test.py", line 25, in <module>
    main()
  File "test.py", line 21, in main
    findImageFiles()
  File "test.py", line 14, in findImageFiles
    findImages = imageRx(someFiles)
TypeError: '_sre.SRE_Pattern' object is not callable

The error is the line
findImages = imageRx(someFiles)


You don't call regexes, you have to use the match or search methods. And you can't call it on a list of file names, you have to call it on each file name separately.

# untested
for filename in someFiles:
    mo = imageRx.search(filename)
    if mo is None:
        # no match
        pass
     else:
        print filename




I incorporated this into my code:

def isimagefile(filename):
   ext = os.path.splitext(filename)[1]
return (ext.lower() in ('.jpg', '.jpeg', '.gif', '.png', '.tif', '.tiff'))


And it's working fine now. Thanks! :-)

Dayo
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to