[Tutor] A simpler mousetrap

Liam Clarke cyresse at gmail.com
Thu Dec 16 09:24:45 CET 2004


Hi all, 

I'm writing some code, and I want to take a given path + filename, and
change the file extension to *.bak.

In doing so, (entirely internal this function), I am assuming -

That the file will always have an extension
Thathe extension will vary
But, it will follow the general DOS format of name.ext

So, I came up with this -

a="./tc/arc/gab.pct"

x=a.replace(a[a.rfind('.'):len(a)],'.bak')

x="./tc/arc/gab.bak"

So, it works, but it feels a bit, well, hacky to me. Not nearly hacky
as using an regex though ; )

I thought about 

a="./tc/arc/gab.pct"

aList=a.split('.')
aList[-1]='bak'
a=".".join(aList)

but I'm wondering if there's a simpler way, as there usually seems to
be, and it's always so obvious once I'm shown it, like 6 down - Six on
vehicle live in the manse (VI + car). Obvious once you know.

Regards,

Liam Clarke
-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list