468,512 Members | 1,514 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

sys.argv[0] doesn't always contain the full path of running script.

Hi,

I use RedHat linux.

How can I find where exactly the current python script is running?

I use this code:

#test.py
import os,sys
print sys.argv
os.chdir(os.path.dirname(sys.argv[0]))
It doesn't work when I run this command from the directory that
test.py is located:

python test.py

That means sys.argv[0] doesn't always contain the full path of
running script.

Any help would be appreciated,
Max

Aug 29 '06 #1
7 4333
Hello Max,
How can I find where exactly the current python script is running?
...
That means sys.argv[0] doesn't always contain the full path of
running script.
sys.path[0] is the script directory, combined with sys.argv[0] you can
find the full path to the script.
(Note that in some rare cases sys.path[0] might not contain the script
directory. For example in an executable created by py2exe).

HTH.

--
Miki
http://pythonwise.blogspot.com

Aug 29 '06 #2
>How can I find where exactly the current python script is running?
>...
That means sys.argv[0] doesn't always contain the full path of
running script.
sys.path[0] is the script directory, combined with sys.argv[0] you can
find the full path to the script.
(Note that in some rare cases sys.path[0] might not contain the script
directory. For example in an executable created by py2exe).
I am not sure it is a good idea to rely on sys.path[0] being the current
directory.

I think the proper solution is to use: os.path.abspath(sys.argv[0])
If sys.argv[0] is a relative path, than adding cwd via the above function
will make it absolute as the gp wanted.

This may only break if the python program messes around with the cwd but in
that case its a good idea to extract os.path.abspath(sys.argv[0]) before
that.

Aug 29 '06 #3

gmax2006 wrote:
Hi,

I use RedHat linux.

How can I find where exactly the current python script is running?
Hi,

Doesnt __file__ attribute of each module contain the full filepath of
the module?
So, try this:

filepath = __file__
print filepath

Works for me :)

Cheers,
i. zuzak

Aug 30 '06 #4
>How can I find where exactly the current python script is running?
>
Doesnt __file__ attribute of each module contain the full filepath of
the module?
Yes indeed! But the path to the module will not be the same as the path to
the script if you are currently in an imported module. Consider this:

my_script.py:
---------------------------
import my_module

---------------------------

my_module.py:
---------------------------
print __file__

---------------------------

Running "python test.py" now prints /path/to/my_module.py, not
/path/to/my_script.py.

Cheers!
/Joel Hedlund
Aug 31 '06 #5
Running "python test.py" now prints /path/to/my_module.py, not
/path/to/my_script.py.
That should have been "python my_script.py". Sorry for the slip-up.

Cheers!
/Joel
Aug 31 '06 #6

Joel Hedlund wrote:
Yes indeed! But the path to the module will not be the same as the path to
the script if you are currently in an imported module. Consider this:
I thought that was the point - to get the full path of the running
script? I see you use the terms "script" and "module" in different
contexts, while I use them as: script = module = file. I can't say
which is right, though :).

Cheers,
ivan

Aug 31 '06 #7

Ivan Zuzak wrote:
Joel Hedlund wrote:
Yes indeed! But the path to the module will not be the same as the path to
the script if you are currently in an imported module. Consider this:

I thought that was the point - to get the full path of the running
script? I see you use the terms "script" and "module" in different
contexts, while I use them as: script = module = file. I can't say
which is right, though :).
If you execute fubar.py, it's a script with __name__ == "__main__"; if
you import it, it's a module __name__ == "fubar".

Aug 31 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by fowlertrainer | last post: by
9 posts views Thread by Daniel Schüle | last post: by
149 posts views Thread by Christopher Benson-Manica | last post: by
2 posts views Thread by johnny | last post: by
2 posts views Thread by Thomas W | last post: by
2 posts views Thread by Hai Vu | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.