os.path.splitext() and case sensitivity

Juho Schultz juho.schultz at helsinki.fi
Wed Dec 21 08:33:31 EST 2005


rbt wrote:
> Hi,
> 
> Is there a way to make os.path.splitext() case agnostic?
> 
> def remove_file_type(target_dir, file_type):
>     for root, dirs, files in os.walk(target_dir):
>         for f in files:
>             if os.path.splitext(os.path.join(root, f))[1] in file_type:
>                 pass
> 
> remove_file_type(sysroot, ['.tmp', '.TMP'])
> 
> As you can see, the way I do it now, I place file extensions in a list. 
> However, I'd like to able just to say '.tmp' and for that to work on any 
> type of file that has tmp (no matter the case) in the extension.
> 
> Many thanks!!!


One solution would be to convert the extensions to lowercase
(or uppercase, if you prefer that)

if fileExtension.lower() == ".tmp":



More information about the Python-list mailing list