wxFileName.GetName()

Hi all,

I'm using a notepad to launch multiple text documents. When I do an "Open"
or "Save As" I want to use the notebook's method SetPageText to set the name
to match the filename (minus the extension). The easiest way looks like
it'll be something the docs show as wxFileName.GetName(), but I can't figure
out how to use it.

If I get either the path (including filename) or filename (including
extension) down as far as a variable - how do I execute this command to
extract just the filename? Is there a special sub-module of wxPython I have
to import first? Is this one of those commands that uses an underscore
instead of a dot seperator?

More importantly, is there a way for me to identify these facts on my own
instead of always having to ask? The docs don't mention anything special
that I can see, but then I'm still not that good at reading them.

Thanks
-Rick Zoerner

Rick Zoerner wrote:

Hi all,

I'm using a notepad to launch multiple text documents. When I do an "Open"
or "Save As" I want to use the notebook's method SetPageText to set the name
to match the filename (minus the extension). The easiest way looks like
it'll be something the docs show as wxFileName.GetName(), but I can't figure
out how to use it.

If I get either the path (including filename) or filename (including
extension) down as far as a variable - how do I execute this command to
extract just the filename? Is there a special sub-module of wxPython I have
to import first? Is this one of those commands that uses an underscore
instead of a dot seperator?

Look at the documentation for Python module path (os.path).

More importantly, is there a way for me to identify these facts on my own
instead of always having to ask? The docs don't mention anything special
that I can see, but then I'm still not that good at reading them.

Thanks
-Rick Zoerner

Check the documetation before asking.

Uwe

Hi Rick,

I think this should help:

import os.path

file_name_with_ext = wxFileName.GetName()
file_name_no_ext, ext = os.path.splitext(file_name_with_ext)

Regards
    Adi

Rick Zoerner wrote:

···

Hi all,

I'm using a notepad to launch multiple text documents. When I do an "Open"
or "Save As" I want to use the notebook's method SetPageText to set the name
to match the filename (minus the extension). The easiest way looks like
it'll be something the docs show as wxFileName.GetName(), but I can't figure
out how to use it.

If I get either the path (including filename) or filename (including
extension) down as far as a variable - how do I execute this command to
extract just the filename? Is there a special sub-module of wxPython I have
to import first? Is this one of those commands that uses an underscore
instead of a dot seperator?

More importantly, is there a way for me to identify these facts on my own
instead of always having to ask? The docs don't mention anything special
that I can see, but then I'm still not that good at reading them.

Thanks
-Rick Zoerner

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

Thanks everyone for all the responses. I hadn't thought of os.path because I
wasn't thinking of the object to be parsed as a path, but rather a filename
( was using a wxFileDialog and pulling GetFileName - so I was already down
to "name.ext" only). Using os.path.splittext works nicely.

As an aside, I still think being able to use wxFileName.GetName() would be a
more elegant solution since according to the documentation (attention Uwe -
I do spend hours pouring through documentation before asking questions) it
should directly return exactly what I'm looking for - the filename already
stripped of the extension. Also, I was hoping it would in some way allow me
to nest the functions to a single line.

name,ext = os.path.splittext(wxFileDialog.GetFileName())

doesn't work. I have to first assign GetFileName to a variable, and then
pass it to os.path.splittext.

I was never able to get it (wxFileName.GetName())to work, though, because
any attempt to call or assign wxFileName generates an error of "global
variable wxFileName doesn't exist" type, and any atttempt to replace
wxFileName with the filename or window or parent (etc, etc.) results in
errors that "(the object) has no attribute GetFileName".

Rick Zoerner wrote:

name,ext = os.path.splittext(wxFileDialog.GetFileName())

doesn't work. I have to first assign GetFileName to a variable, and then
pass it to os.path.splittext.

Are you sure? this really should work...

I was never able to get it (wxFileName.GetName())to work, though, because
any attempt to call or assign wxFileName generates an error of "global
variable wxFileName doesn't exist" type

As a rule, wxWindows classes that are replications of basic python functionality are not wrapped for wxPython. This may be one of those cases.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov