Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > os.path.splitext() and case sensitivity

Reply
Thread Tools

os.path.splitext() and case sensitivity

 
 
rbt
Guest
Posts: n/a
 
      12-21-2005
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!!!
 
Reply With Quote
 
 
 
 
Juho Schultz
Guest
Posts: n/a
 
      12-21-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":
 
Reply With Quote
 
 
 
 
Richie Hindle
Guest
Posts: n/a
 
      12-21-2005

[rbt]
> 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'])


def remove_file_type(target_dir, file_type):
[...]
if os.path.splitext(f)[1].lower() == file_type.lower():
pass

remove_file_type(sysroot, '.tmp')

--
Richie Hindle
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
rbt
Guest
Posts: n/a
 
      12-21-2005
Richie Hindle wrote:
> [rbt]
>> 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'])

>
> def remove_file_type(target_dir, file_type):
> [...]
> if os.path.splitext(f)[1].lower() == file_type.lower():
> pass
>
> remove_file_type(sysroot, '.tmp')
>


Thanks guys!!!
 
Reply With Quote
 
rbt
Guest
Posts: n/a
 
      12-21-2005
Juho Schultz wrote:
> 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":


Many thanks... I did it this way as I sometimes delete files with
different extensions:

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].lower() in
file_type:


remove_file_type(user_docs, ['.tmp', '.mp3'])
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Eclipse Search and Replace with Emacs like Case Sensitivity eschreiber@gmail.com Java 0 01-25-2005 04:42 PM
question on URL case sensitivity Kev HTML 15 01-03-2005 05:36 AM
Case Sensitivity of Component Names Not Consistent? Harry Whitehouse ASP .Net 0 09-15-2004 01:08 AM
case sensitivity for Thunderbird message filter Maleki Firefox 1 08-26-2004 06:26 AM
Thunderbird Message Filter Case Sensitivity Maleki Firefox 0 08-16-2004 02:48 PM



Advertisments