[Tutor] scratching my head

Clayton Kirkwood crk at godblessthe.us
Mon Aug 3 00:28:06 CEST 2015



> -----Original Message-----
> From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
> Behalf Of Alan Gauld
> Sent: Sunday, August 02, 2015 3:01 PM
> To: tutor at python.org
> Subject: Re: [Tutor] scratching my head
> 
> On 02/08/15 22:44, Clayton Kirkwood wrote:
> 
> > for dir_path, directories, files in os.walk(main_dir):
> >      for file in files:
> > #        print( " file = ", file)
> > #       if( ("(\.jpg|\.png|\.avi|\.mp4)$") not in file.lower() ):
> 
> Python sees that as a single string. That string is not in your filename.
> 
> > #        if(  (".jpg" or ".png" or ".avi" or ".mp4" )  not in
file.lower()
> 
> Python sees that as a boolean expression so will try to work it out as a
> True/False value. Since a non empty string is considered True and the
first
> True expression makes an OR opeation True overall it returns ".jpg" and
tests
> if it is not in the filename.
> 
> > #except by the drudgery below. I should be able to just have a list,
> > maybe from a file, that lists all
> 
> You might think so but that's not how 'in' works.
> 
> But you could use a loop:
> 
> found = False
> for s in (".jpg",".png",".avi",".mp4"):
>      found = test or (s in file.lower()) if not found: ...


The for is much better and it's able to get input from a file. I would think
Python more sensible if something like my commented one would work. That
would make more sense to me.

Thanks


> 
> >          if(  ".jpg" not in file.lower() and
> >               ".png" not in file.lower() and
> >               ".avi" not in file.lower() and
> >               ".mp4" not in file.lower() ):
> 
> Whether that's any better than your combined test is a moot point.
> 
> HTH
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list