Yeah, you're right, it looks like that should work for invoking from
the command-line (which was what kurian was asking about).

I guess when I was looking into it, I was trying to solve a slightly
different problem - have a script that would be able to tell what it's
location was, regardless of whether it was invoked from the
command-line, or from execfile (where
os.path.join(sys.path[0],sys.argv[0]) doesn't work).

- Paul

        
Reply
        
        
Hi

On Fri, Apr 24, 2009 at 12:32 PM, Chris G <[email protected]> wrote:
> This seems pretty reliable:
>
> os.path.join(sys.path[0],sys.argv[0])
>
>
> On Fri, Apr 24, 2009 at 10:48 AM, Paul Molodowitch <[email protected]>
> wrote:
>>
>> Unfortunately, I seem to recall being frustrated by this at some
>> point, because for what seems to be a very simple problem, there isn't
>> a very simple solution.  There's a lot of different ways to go about
>> it, but each seems to have some issues:
>>
>> __file__ isn't always defined (forget exactly what situations it won't
>> exist)
>> sys.argv[0] often doesn't have any path info
>> os.getcwd() can vary - you don't have to be in the same cwd as the
>> script you're running
>>
>> I think the 'most' reliable solution I found was:
>>
>> import inspect
>> print inspect.getsourcefile( lambda:None )
>>
>> (grabbed from this thread:
>> http://mail.python.org/pipermail/python-list/2003-December/242365.html
>> - jason.osipa was also hinting this way, I think)
>>
>> However, I think there are situations where this can fail, too (again,
>> don't remember exactly - I think when using .pyc's, that have
>> subsequently moved, perhaps? I remember that was tripping up one of
>> these methods, anyway.)
>>
>>
>>
>> 2009/4/24 Bård Henriksen <[email protected]>:
>> >
>> > use __file__
>> >
>> > eg:
>> >
>> > def get_my_path():
>> >        """
>> >                Return the path to wherever this script file is stored
>> >        """
>> >        return os.path.dirname(__file__)
>> >
>> > Bård
>> >
>> > >
>> >
>>
>>
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/python_inside_maya
-~----------~----~----~----~------~----~------~--~---

    • ... kurian os ™ ®കോപ്പിയടിച്ചാല്©ഗോതമ്പുണ്ട!
      • ... pierrotb
        • ... kurian os ™ ®കോപ്പിയടിച്ചാല്©ഗോതമ്പുണ്ട!
          • ... pierrotb
            • ... Chad Dombrova
              • ... kurian os ™ ®കോപ്പിയടിച്ചാല്©ഗോതമ്പുണ്ട!
              • ... jason . osipa
  • ... Bård Henriksen
    • ... Paul Molodowitch
      • ... Chris G
        • ... Paul Molodowitch
          • ... kurian os ™ ®കോപ്പിയടിച്ചാല്©ഗോതമ്പുണ്ട!

Reply via email to