os.path.splitext() and case sensitivity

R

rbt

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!!!
 
J

Juho Schultz

rbt said:
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":
 
R

Richie Hindle

[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')
 
R

rbt

Richie said:
[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!!!
 
R

rbt

Juho said:
rbt said:
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'])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top