473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

os.path.splitext() and case sensitivity

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!!!
Dec 21 '05 #1
4 3937
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":
Dec 21 '05 #2

[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
ri****@entrian.com
Dec 21 '05 #3
rbt
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!!!
Dec 21 '05 #4
rbt
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'])
Dec 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Elliot Temple | last post by:
Hi I have two questions. Could someone explain to me why Python is case sensitive? I find that annoying. Also, why aren't there multiline comments? Would adding them cause a problem of some...
70
by: Michael Hoffman | last post by:
Many of you are familiar with Jason Orendorff's path module <http://www.jorendorff.com/articles/python/path/>, which is frequently recommended here on c.l.p. I submitted an RFE to add it to the...
2
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string...
8
by: Lad | last post by:
Hello, what is a way to get the the extension of a filename from the path? E.g., on my XP windows the path can be C:\Pictures\MyDocs\test.txt and I would like to get the the extension of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.