Re: [Tutor] Regex to find files ending with one of a given set of extensions

2010-02-22 Thread Dayo Adewunmi

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 
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


Re: [Tutor] Regex to find files ending with one of a given set of extensions

2010-02-21 Thread Steven D'Aprano
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 
> 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




-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Regex to find files ending with one of a given set of extensions

2010-02-21 Thread Dayo Adewunmi

Hi all

I'm trying use regex to match image formats:

import re

def findImageFiles():
   imageRx = re.compile('\.jpe?g$|\.png$|\.gif$|\.tiff?$', re.I)

   someFiles = 
["sdfinsf.png","dsiasd.dgf","wecn.GIF","iewijiefi.jPg","iasjasd.py"]


   findImages = imageRx(someFiles)

   print "START: %s" %(findImages.start())
   print "GROUP: %s" %(findImages.group())


def main():
   findImageFiles()


if __name__ == "__main__":
   main()


here's the traceback:

$ python test.py
Traceback (most recent call last):
 File "test.py", line 25, in 
   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

 i'm new with regexing, please help.

Thanks

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