Failed to load icon from the file "%1"

Hello,

I use TheMimeTypesManager in order to get the icon associated with a file
extension. I made the following function:

def GetIcon(extension):
    fileType = wx.TheMimeTypesManager.GetFileTypeFromExtension(extension)
    if not fileType:
        return
    info = fileType.GetIconInfo()
    icon = None
    if info:
        icon, file, idx = info
        if not icon.Ok():
            icon = None
    return icon

Sometimes I get the following error: Failed to load icon from the file "%1"
(error 2: the system can not find the file specified.). It's displayed in a
dialog box (probably a C++ error). Is there a way to avoid this in python?

Thanks in advance,

M. Vernier

M. Vernier wrote:

  Hello,

I use TheMimeTypesManager in order to get the icon associated with a file
extension. I made the following function:

def GetIcon(extension):
    fileType = wx.TheMimeTypesManager.GetFileTypeFromExtension(extension)
    if not fileType:
        return
    info = fileType.GetIconInfo()
    icon = None
    if info:
        icon, file, idx = info
        if not icon.Ok():
            icon = None
    return icon

Sometimes I get the following error: Failed to load icon from the file "%1"
(error 2: the system can not find the file specified.). It's displayed in a
dialog box (probably a C++ error). Is there a way to avoid this in python?

To just supress the log message you can create an instance of wx.LogNull, when that instance is deleted then the old logger is restored.

To fix the icon problem will probably take editing either the registry or the various mime config files if on a non-Windows platform.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

HI
Can anyone tell me how to find out the path of the executing program?
Thank in advance
Greg

os.getcwd()
"Return a string representing the current working directory. Availability: Macintosh, Unix, Windows."

···

On Mon, 17 May 2004 21:17:27 +1000, Greg Binns <gregbin@bigpond.net.au> wrote:

HI
Can anyone tell me how to find out the path of the executing program?
Thank in advance
Greg

--
Peter Damoc
Hacker Wannabe

Peter Damoc wrote:

HI
Can anyone tell me how to find out the path of the executing program?
Thank in advance
Greg

os.getcwd()
"Return a string representing the current working directory. Availability: Macintosh, Unix, Windows."

That's NOT always the path of the executing program.

Rather try the following:

import os
import sys

dirname = os.path.dirname(sys.argv[0])
if dirname and dirname != os.curdir:
    print dirname
else:
    print os.getcwd()

(You have to do it differently if your application is frozen)

···

On Mon, 17 May 2004 21:17:27 +1000, Greg Binns <gregbin@bigpond.net.au> > wrote:

--
charl p. botha http://cpbotha.net/ http://visualisation.tudelft.nl/

Can anyone tell me how to find out the path of the executing program?

os.getcwd()
"Return a string representing the current working directory. Availability: Macintosh, Unix, Windows."

That's NOT always the path of the executing program.
Rather try the following:

import os
import sys
dirname = os.path.dirname(sys.argv[0])
if dirname and dirname != os.curdir:
   print dirname
else:
   print os.getcwd()

I recommend doing os.path.abspath(os.path.dirname(sys.argv[0]))

Also, sys.path[0] contains the path of the directory where
the program was found. Scroll down the "path" on this page

     <http://docs.python.org/lib/module-sys.html&gt;

/Jean Brouwers
  ProphICy Semiconductor, Inc.

John Hoffman wrote:

···

Can anyone tell me how to find out the path of the executing program?

os.getcwd()
"Return a string representing the current working directory. Availability: Macintosh, Unix, Windows."

That's NOT always the path of the executing program.
Rather try the following:

import os
import sys
dirname = os.path.dirname(sys.argv[0])
if dirname and dirname != os.curdir:
   print dirname
else:
   print os.getcwd()

I recommend doing os.path.abspath(os.path.dirname(sys.argv[0]))

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

>>>> Can anyone tell me how to find out the path of the executing

program?

>>>
>>> os.getcwd()
>>> "Return a string representing the current working directory.
>>> Availability: Macintosh, Unix, Windows."
>>
>> That's NOT always the path of the executing program.
>> Rather try the following:
>>
>> import os
>> import sys
>> dirname = os.path.dirname(sys.argv[0])
>> if dirname and dirname != os.curdir:
>> print dirname
>> else:
>> print os.getcwd()
>
>
> I recommend doing os.path.abspath(os.path.dirname(sys.argv[0]))
>

Also, sys.path[0] contains the path of the directory where
the program was found. Scroll down the "path" on this page

     <http://docs.python.org/lib/module-sys.html&gt;

I have a related question: I have a module that is called by several
different programs. I want that module to save data related to itself
in its own directory, so there are not multiple copies for each of the
calling programs. Is there a way to get the path of that module when
another program calls it (other than hard-coding)?

Assuming the module is loaded from a directory, you might
be able to find the directory of that module by walking
the sys.path list. Something like

   import os, sys
   for d in sys.path:
       try:
           m = os.path.join(d, <module_file_name>)
           if os.access(m, os.F_OK):
               break # module exists in directory d
       except OSError:
           pass
   else:
       pass # not found

But if the module is loaded from a .zip file or some other
site-specific location, this code above will not find it.

/Jean Brouwers
  ProphICy Semiconductor, Inc.

John Li wrote:

···

Can anyone tell me how to find out the path of the executing

program?

os.getcwd()
"Return a string representing the current working directory. Availability: Macintosh, Unix, Windows."

That's NOT always the path of the executing program.
Rather try the following:

import os
import sys
dirname = os.path.dirname(sys.argv[0])
if dirname and dirname != os.curdir:
  print dirname
else:
  print os.getcwd()

I recommend doing os.path.abspath(os.path.dirname(sys.argv[0]))

Also, sys.path[0] contains the path of the directory where
the program was found. Scroll down the "path" on this page

    <http://docs.python.org/lib/module-sys.html&gt;

I have a related question: I have a module that is called by several
different programs. I want that module to save data related to itself
in its own directory, so there are not multiple copies for each of the
calling programs. Is there a way to get the path of that module when
another program calls it (other than hard-coding)?

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

# __file__ is path to module file or to package __init__.py.
# From within module:
configPath = os.path.dirname( __file__ )

# From outside module
configPath = os.path.dirname( sharedModule.__file__ )

- Sam

···

At 2004-05-17 03:47 PM -0400, you wrote:

I have a related question: I have a module that is called by several
different programs. I want that module to save data related to itself
in its own directory, so there are not multiple copies for each of the
calling programs. Is there a way to get the path of that module when
another program calls it (other than hard-coding)?

__________________________________________________________
Spinward Stars, LLC Samuel Reynolds
Software Consulting and Development 303-805-1446
http://SpinwardStars.com/ sam@SpinwardStars.com

Jean Brouwers wrote:

Assuming the module is loaded from a directory,

Which is actually an assumption that you often don't want to make. If you are able to make the assumption, the __file__ module attribute is normally fine for this kind of use.

<promotion style=shameless>
If not, and you need a general solution, rather than one that each application needs to know about, ResourcePackage is designed explicitly to solve this problem (embedding resources in packages intended to be used in large number of applications where the known environment will include simple installation of the package, py2exe, McMillan Installer etceteras).

ResourcePackage is, in essence, just an automated version of the img2py script from wxPython. It scans a directory for changed resource files (non-Python files) on import and runs the equivalent of img2py for each found resource. You simply save an updated version of the resource file to the directory and then run the app to get the new version of the resource available (the first import of the resource-package does the updating, so all new resources are updated at that time).

To use your resources, you simple import them like so:

    from myreusablelibrary.resources import someicon_png
    data = someicon_png.data

That works for any data-type which can be loaded from an in-memory string (notable exception being .wav files at the moment due to a bug in current wxPython); it won't work for data-types which are only loadable via filename interfaces.

    http://resourcepackage.sf.net/
</promotion>

BTW, there's an alternate reading of the question which is not about resources to distribute with the app, but how to determine where the program should store user-preferences or the like while it's running. Normally that is platform specific, with the Windows version using one of the registered shell locations and Unix-ish systems using a .library directory in the user's home directory. You should *not* generally store such information in any path which is readily constructed as a relative transform from the module's __file__. There's some basic code for this kind of thing in OpenGLContext.

HTH,
Mike
...

I have a related question: I have a module that is called by several
different programs. I want that module to save data related to itself
in its own directory, so there are not multiple copies for each of the
calling programs. Is there a way to get the path of that module when
another program calls it (other than hard-coding)?

...

···

________________________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/
  blog: http://zope.vex.net/~mcfletch/plumbing/

Mike,

Thank you -and Samuel Reynolds- for pointing to the module __file__
attribute. I was not aware of that. It is obviously the answer to
the orginal question, far better than my clumsy example.

Similarly, thank you for the pointer to the ResourcePackage.

/Jean Brouwers
  ProphICy Semiconductor, Inc.

Mike C. Fletcher wrote:

···

Jean Brouwers wrote:

Assuming the module is loaded from a directory,

Which is actually an assumption that you often don't want to make. If you are able to make the assumption, the __file__ module attribute is normally fine for this kind of use.

<promotion style=shameless>
If not, and you need a general solution, rather than one that each application needs to know about, ResourcePackage is designed explicitly to solve this problem (embedding resources in packages intended to be used in large number of applications where the known environment will include simple installation of the package, py2exe, McMillan Installer etceteras).

ResourcePackage is, in essence, just an automated version of the img2py script from wxPython. It scans a directory for changed resource files (non-Python files) on import and runs the equivalent of img2py for each found resource. You simply save an updated version of the resource file to the directory and then run the app to get the new version of the resource available (the first import of the resource-package does the updating, so all new resources are updated at that time).

To use your resources, you simple import them like so:

   from myreusablelibrary.resources import someicon_png
   data = someicon_png.data

That works for any data-type which can be loaded from an in-memory string (notable exception being .wav files at the moment due to a bug in current wxPython); it won't work for data-types which are only loadable via filename interfaces.

   http://resourcepackage.sf.net/
</promotion>

BTW, there's an alternate reading of the question which is not about resources to distribute with the app, but how to determine where the program should store user-preferences or the like while it's running. Normally that is platform specific, with the Windows version using one of the registered shell locations and Unix-ish systems using a .library directory in the user's home directory. You should *not* generally store such information in any path which is readily constructed as a relative transform from the module's __file__. There's some basic code for this kind of thing in OpenGLContext.

HTH,
Mike
...

I have a related question: I have a module that is called by several
different programs. I want that module to save data related to itself
in its own directory, so there are not multiple copies for each of the
calling programs. Is there a way to get the path of that module when
another program calls it (other than hard-coding)?

...

________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
blog: http://zope.vex.net/~mcfletch/plumbing/

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

Hello!

Just a small note from my expirence. If you later happened to use py2exe for
converting your application to standalone executable, then beware that under
py2exe:

1) sys.argv[0] will contains just the filename of executable (without the
path), for example: hello.exe
2) __file__ variable does not exists

    Vladimir Ignatov

Vladimir Ignatov wrote:

Just a small note from my expirence. If you later happened to use py2exe for
converting your application to standalone executable, then beware that under
py2exe:

1) sys.argv[0] will contains just the filename of executable (without the
path), for example: hello.exe

It depends on how you run your application. Tru to run it as C:\PROGRA~1\Hello\hello.exe and you will have full path in sys.argv[0]

···

--
Oleg Deribas,
http://copi.ru/14000/