By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,714 Members | 750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,714 IT Pros & Developers. It's quick & easy.

find a .py path

P: n/a
hg
Hi,

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

Thanks,

hg

Jan 5 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
hg
hg wrote:
Hi,

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

Thanks,

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

hg

Jan 5 '07 #2

P: n/a
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
Jan 5 '07 #3

P: n/a
hg
Laszlo Nagy wrote:
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
Thanks

Jan 5 '07 #4

P: n/a
hg írta:
hg wrote:

>Hi,

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

Thanks,

hg

oops: os.path.abspath(sys.argv[0])
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

Jan 5 '07 #5

P: n/a
Hi,

What about :

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

hg a écrit :
Hi,

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

Thanks,

hg
Jan 5 '07 #6

P: n/a
On 5 ene, 13:33, Laszlo Nagy <gand...@designaproduct.bizwrote:
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

Jan 7 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.