As Wolfram pointed out, os.path.splitext() is a good way to do this. It is also more robust than the other solutions because it does the right thing if there is no extension on the original file name. I just want to say that your first solution can be written much more simply as
x=a[:a.rfind('.')] + '.bak'


Kent

Liam Clarke wrote:
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
_______________________________________________
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to