Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

find a .py path

0 views
Skip to first unread message

hg

unread,
Jan 5, 2007, 4:09:00 AM1/5/07
to
Hi,

Is there an easy way for a script being executed (from anywhere) to know
where is is ... something in os.path ?

Thanks,

hg

hg

unread,
Jan 5, 2007, 4:11:49 AM1/5/07
to
hg wrote:

oops: os.path.abspath(sys.argv[0])

hg

Laszlo Nagy

unread,
Jan 5, 2007, 11:16:08 AM1/5/07
to hg, pytho...@python.org
hg i'rta:

> Hi,
>
> Is there an easy way for a script being executed (from anywhere) to know
> where is is ... something in os.path ?
>
__file__

You can also use os.argv[0] but that is not the same...

Laszlo

hg

unread,
Jan 5, 2007, 4:29:24 AM1/5/07
to
Laszlo Nagy wrote:

Thanks

Laszlo Nagy

unread,
Jan 5, 2007, 11:33:09 AM1/5/07
to hg, pytho...@python.org
hg írta:
Be aware with this. It is different when you do

/usr/bin/python prog.py

and

./prog.py

In the first case, sys.argv[0] will be /usr/bin/python!

You should better use

os.path.abspath(__file__)

(that will work for normal python programs, but not for py2exe compiled
programs or for modules imported from a zip file...)

Best,

Laszlo

laurent rahuel

unread,
Jan 5, 2007, 4:25:20 PM1/5/07
to
Hi,

What about :

import os.path
print os.path.abspath(__file__)

hg a écrit :

Gabriel Genellina

unread,
Jan 7, 2007, 2:07:56 PM1/7/07
to
On 5 ene, 13:33, Laszlo Nagy <gand...@designaproduct.biz> wrote:

> Be aware with this. It is different when you do
>
> /usr/bin/python prog.py
>
> and
>
> ./prog.py
>
> In the first case, sys.argv[0] will be /usr/bin/python!

No, sys.argv[0] is always the running script, and sys.argv[1] the first
argument after the script, and so on. It doesn't matter if you call
python implicitely, or with other options. Try this:
/usr/bin/python -i -u -O prog.py

--
Gabriel Genellina

0 new messages