[Tutor] scratching my head

Cameron Simpson cs at zip.com.au
Mon Aug 3 03:02:42 CEST 2015


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.

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

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Tutor mailing list