[Tutor] scratching my head

Clayton Kirkwood crk at godblessthe.us
Mon Aug 3 03:33:30 CEST 2015



> -----Original Message-----
> From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
> Behalf Of Cameron Simpson
> Sent: Sunday, August 02, 2015 6:03 PM
> To: tutor at python.org
> Subject: Re: [Tutor] scratching my head
> 
> On 02Aug2015 16:15, Clayton Kirkwood <crk at godblessthe.us> wrote:
> >> Behalf Of Cameron Simpson
> >> Sent: Sunday, August 02, 2015 3:35 PM
> [...]
> >> Personally I'd be reaching for os.path.splitext. Untested example
below:
> >>
> >>   from os.path import splitext
> >>   ....
> >>   for dir_path, directories, files in os.walk(main_dir):
> >>     for file in files:
> >>       prefix, ext = splitext(file)
> >>       if ext and ext[1:].lower() in ('jpg', 'png', 'avi', 'mp4'):
> >>         ....
> >>
> >> which I think is much easier to read.
> >>
> >> BTW, I'd be using the variable names "filename" and "filenames"
> >> instead of "file" and "files": in python 2 "file" is a builtin
> >> function (though long deprecated by "open()") and in any case I'd
> >> (personally) expect such a
> >name
> >> to be an _open_ file. As opposed to "filename", which is clearer.
> >
> >Thanks, that should also help a lot. Now time to look at splitext, and
> >the ext and ext[1:.
> 
> The "[1:]" is because "ext" will include the dot.

Yeah, after looking it up, it became clear, but thanks!

> 
> >I appreciate your comments also about the variable names.
> >Any comments on the problems lower in the file?
> 
> Maybe you'd better reraise these problems again explicitly.

Point taken.

> 
> Cheers,
> Cameron Simpson <cs at zip.com.au>
> _______________________________________________
> 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