save a file in the same name but with different extension

Hi, I want to open a file and after a simple processing, save the
result in a file with the same name but with different extension. For
example, open a file name "test.bmp" and save the result in
"test.txt". I used the GetPath method of wx.FileDialog, but I know the
resultant string contains the extension of the opened file, "bmp" in
my example. So I use GetPath().[:-3]+"txt" to get the output
name automatically. I know it is quite stupid, and there must be a
built-in, smarter way to make it. In this case, I know the input file
extension is three letters long, but if the file is someting like
"test.tiff", then this method gives a wrong result.

Any help is appreciated.

Hi,

···

On Mon, Mar 29, 2010 at 7:52 AM, seasoul <bioseasoul@gmail.com> wrote:

Hi, I want to open a file and after a simple processing, save the
result in a file with the same name but with different extension. For
example, open a file name "test.bmp" and save the result in
"test.txt". I used the GetPath method of wx.FileDialog, but I know the
resultant string contains the extension of the opened file, "bmp" in
my example. So I use GetPath().[:-3]+"txt" to get the output
name automatically. I know it is quite stupid, and there must be a
built-in, smarter way to make it. In this case, I know the input file
extension is three letters long, but if the file is someting like
"test.tiff", then this method gives a wrong result.

filename = 'foo.bar'
newname = filename.rsplit('.', 1)[0] + '.txt'

This one works too:

newname = os.path.splitext(filename)[0] + ".txt"

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

==> Never *EVER* use RemovalGroup for your house removal. You'll
regret it forever. <==

···

On 29 March 2010 13:20, Cody Precord wrote:

Hi,

On Mon, Mar 29, 2010 at 7:52 AM, seasoul <bioseasoul@gmail.com> wrote:

Hi, I want to open a file and after a simple processing, save the
result in a file with the same name but with different extension. For
example, open a file name "test.bmp" and save the result in
"test.txt". I used the GetPath method of wx.FileDialog, but I know the
resultant string contains the extension of the opened file, "bmp" in
my example. So I use GetPath().[:-3]+"txt" to get the output
name automatically. I know it is quite stupid, and there must be a
built-in, smarter way to make it. In this case, I know the input file
extension is three letters long, but if the file is someting like
"test.tiff", then this method gives a wrong result.

filename = 'foo.bar'
newname = filename.rsplit('.', 1)[0] + '.txt'

Oh, that works, thanks a lot, Andrea.

By the way, from the link in your signature, I found you are the “Infinity 77” owner. Oh my god, when I just started learning python and pywxPython one month ago, I already found your webiste is really really helpful. A bunch of amazing gadgets are written by you. Thanks for your work to the python community.

···

On Mon, Mar 29, 2010 at 9:30 PM, Andrea Gavana andrea.gavana@gmail.com wrote:

On 29 March 2010 13:20, Cody Precord wrote:

Hi,

On Mon, Mar 29, 2010 at 7:52 AM, seasoul bioseasoul@gmail.com wrote:

Hi, I want to open a file and after a simple processing, save the

result in a file with the same name but with different extension. For
example, open a file name “test.bmp” and save the result in
“test.txt”. I used the GetPath method of wx.FileDialog, but I know the

resultant string contains the extension of the opened file, “bmp” in
my example. So I use GetPath().[:-3]+“txt” to get the output
name automatically. I know it is quite stupid, and there must be a

built-in, smarter way to make it. In this case, I know the input file
extension is three letters long, but if the file is someting like
“test.tiff”, then this method gives a wrong result.

filename = ‘foo.bar’
newname = filename.rsplit(‘.’, 1)[0] + ‘.txt’

This one works too:

newname = os.path.splitext(filename)[0] + “.txt”

Andrea.

“Imagination Is The Only Weapon In The War Against Reality.”
http://xoomer.alice.it/infinity77/

==> Never EVER use RemovalGroup for your house removal. You’ll

regret it forever. <==


To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

To unsubscribe from this group, send email to wxpython-users+unsubscribegooglegroups.com or reply to this email with the words “REMOVE ME” as the subject.


Yours Sincerely
Ba Te