[Tutor] python function to know the path of the program in execution *
David Hutto
dwightdhutto at yahoo.com
Fri Feb 5 14:13:18 CET 2010
If you want to know the file you're script is running from then print
sys.argv[0]
this is the script I ran from a file named lowerdir.py:
import sys
print
"This is sys.path:"
print
print sys.path
print
print "This
is sys.path[0], the current working directory:"
print
print
sys.path[0]
print
print "This is sys.path[1], the first in the
PYTHONOPATH string after the current directory your script is running
in:"
print
print sys.path[1]
print
print "This is
sys.path[-1] the last in the PYTHONPATH list:"
print
print
sys.path[-1]
print
print "This is sys.argv[0] your current running
scripts full path:"
print
print sys.argv[0]
This is my
output:
IDLE 2.6.4
>>>
================================ RESTART
================================
>>>
This is sys.path:
['C:\\Python26\\testingscripts\\lowerdir',
'C:\\Python26\\Lib\\idlelib', 'C:\\Windows\\system32\\python26.zip',
'C:\\Python26\\DLLs', 'C:\\Python26\\lib',
'C:\\Python26\\lib\\plat-win', 'C:\\Python26\\lib\\lib-tk',
'C:\\Python26', 'C:\\Python26\\lib\\site-packages',
'C:\\Python26\\lib\\site-packages\\win32',
'C:\\Python26\\lib\\site-packages\\win32\\lib',
'C:\\Python26\\lib\\site-packages\\Pythonwin']
This is
sys.path[0], the current working directory:
C:\Python26\testingscripts\lowerdir
This
is sys.path[1], the first in the PYTHONOPATH string after the current
directory your script is running in:
C:\Python26\Lib\idlelib
This
is sys.path[-1] the last in the PYTHONPATH list:
C:\Python26\lib\site-packages\Pythonwin
This
is sys.argv[0] your current running scripts full
path:
C:\Python26\testingscripts\lowerdir\lowerdir.py
>>>
As long as you have sys imported at somepoint, then all you
have to do is print sys.argv[0], and that should be the exact name and
location of the current running script,
David
--- On Fri, 2/5/10, Daniel Sarmiento <dsarmientos at gmail.com> wrote:
From: Daniel Sarmiento <dsarmientos at gmail.com>
Subject: Re: [Tutor] python function to know the path of the program in execution *
To: tutor at python.org
Date: Friday, February 5, 2010, 7:53 AM
> Date: Fri, 5 Feb 2010 17:43:37 +0530
> From: Spoorthi <spoorthi.ms at gmail.com>
> To: "Rohit Roger$" <rohitraj007 at gmail.com>
> Cc: tutor <tutor at python.org>
> Subject: Re: [Tutor] python function to know the path of the program
> in execution *
> Message-ID:
> <6c9f52051002050413o57bd76bax7ac5986b17304725 at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> The below data(in blue) made me converge upon sys.path[0] for this
> particular problem...I dont think it returns None if a Python script is
> used. Can anyone please correct me if I am wrong
>
> *As initialized upon program startup, the first item of this list, path[0],
> is the directory containing the script that was used to invoke the Python
> interpreter. If the script directory is not available (e.g. if the
> interpreter is invoked interactively or if the script is read from standard
> input), path[0] is the empty string, which directs Python to search modules
> in the current directory first. Notice that the script directory is inserted
> before the entries inserted as a result of PYTHONPATH.*
>
> On Fri, Feb 5, 2010 at 5:38 PM, Rohit Roger$ <rohitraj007 at gmail.com> wrote:
>
>>
>> sys.path[0] returns none
>>
>>
>>
>> On Fri, Feb 5, 2010 at 5:36 PM, Spoorthi <spoorthi.ms at gmail.com> wrote:
>>
>>> sys.path[0] should be serving the purpose effectively I guess
>>>
>>>
>>> On Fri, Feb 5, 2010 at 5:29 PM, Rohit Roger$ <rohitraj007 at gmail.com>wrote:
>>>
>>>> Answer :
>>>> >>> import sys
>>>>>>> rohit = sys.argv[0]
>>>>>>> print rohit
>>>> it returns the name of the path
>>>>
>>>> On Fri, Feb 5, 2010 at 4:13 PM, David Hutto <dwightdhutto at yahoo.com>wrote:
>>>>
>>>>> Junk Score: 2 out of 10 (below your Auto Allow threshold<https://www.boxbe.com/mail-screening>)
>>>>> | Approve sender <https://www.boxbe.com/anno?tc=1529381613_418589136> |
>>>>> Block sender<https://www.boxbe.com/anno?tc=1529381613_418589136&disp=b>| Block
>>>>> domain <https://www.boxbe.com/anno?tc=1529381613_418589136&disp=b&dom>
>>>>>
>>>>>
>>>>>
>>>>> --- On *Fri, 2/5/10, nikunj badjatya <nikunjbadjatya at gmail.com>* wrote:
>>>>>
>>>>>
>>>>> From: nikunj badjatya <nikunjbadjatya at gmail.com>
>>>>> Subject: [Tutor] python function to know the path of the program in
>>>>> execution
>>>>> To: tutor at python.org
>>>>> Date: Friday, February 5, 2010, 5:08 AM
>>>>>
>>>>> Hi ,
>>>>> Is there any python function to know the path of the python program
>>>>> under execution.?
>>>>> If someone executes a program , he should come to know the path of the
>>>>> program..!!
>>>>> ex. suppose a user ABC is running prog.py which is in ~ directory, the
>>>>> user currently is in XYZ directory.
>>>>> [ ABC at localhost XYZ ]$ python ~/prog.py
>>>>> The program's location is ~/prog.py
>>>>>
>>>>> This should be the outcome of the program.
>>>>>
>>>>> I tried with os.getcwd()
>>>>> os.getcwd() will return /path/to/folder/XYZ.
>>>>>
>>>>> Any suggestions?
>>>>>
>>>>> Thanks,
>>>>> Nikunj Badjatya
>>>>> Bangalore, India
>>>>>
>>>>> -----Inline Attachment Follows-----
>>>>>
>>>>> _______________________________________________
>>>>> Tutor maillist - Tutor at python.org<http://mc/compose?to=Tutor@python.org>
>>>>> To unsubscribe or change subscription options:
>>>>> http://mail.python.org/mailman/listinfo/tutor
>>>>>
>>>>>
>>>>> I think this is what you want:
>>>>>
>>>>>>>> import sys
>>>>>>>> sys.argv[0]
>>>>> 'C:\\Python26\\testingscripts\\lowdir.py'
>>>>>>>>
>>>>>
>>>>> David
>>>>>
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> Tutor maillist - Tutor at python.org
>>>>> To unsubscribe or change subscription options:
>>>>> http://mail.python.org/mailman/listinfo/tutor
>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> Tutor maillist - Tutor at python.org
>>>> To unsubscribe or change subscription options:
>>>> http://mail.python.org/mailman/listinfo/tutor
>>>>
>>>>
>>>
>>>
>>> --
>>> Spoorthi
>>>
>>
>>
>
>
> --
> Spoorthi
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.python.org/pipermail/tutor/attachments/20100205/8272d339/attachment-0001.htm>
>
> ------------------------------
I remember using the __file__ attribute in Django once. I don't know if
that's what you are looking for. You might want to take a loook at
http://pyref.infogami.com/__file__
HTH
Daniel
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100205/e5773b40/attachment-0001.htm>
More information about the Tutor
mailing list