[Tutor] scratching my head
Alan Gauld
alan.gauld at btinternet.com
Mon Aug 3 00:01:05 CEST 2015
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: ...
> 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
More information about the Tutor
mailing list