Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > sys.argv[0] doesn't always contain the full path of running script.

Reply
Thread Tools

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

 
 
gmax2006
Guest
Posts: n/a
 
      08-29-2006
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

 
Reply With Quote
 
 
 
 
Miki
Guest
Posts: n/a
 
      08-29-2006
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

 
Reply With Quote
 
 
 
 
Eyal Lotem
Guest
Posts: n/a
 
      08-29-2006
>> 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.

 
Reply With Quote
 
Ivan Zuzak
Guest
Posts: n/a
 
      08-30-2006

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

 
Reply With Quote
 
Joel Hedlund
Guest
Posts: n/a
 
      08-31-2006
>> 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
 
Reply With Quote
 
Joel Hedlund
Guest
Posts: n/a
 
      08-31-2006
> 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
 
Reply With Quote
 
Ivan Zuzak
Guest
Posts: n/a
 
      08-31-2006

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

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      08-31-2006

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".

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
os.path.walk() to get full path of all files dude Python 5 03-17-2011 09:09 AM
RCR: String#contain? and Array#contain? Roger Pack Ruby 3 09-28-2010 04:13 PM
Does string contain A, and if so, does a section of string contain B Jason Carlton Javascript 11 12-08-2009 06:07 PM
How can I retrieve the full path of running exe somank.sharma@gmail.com C Programming 4 06-21-2007 09:07 AM
Simple way to get the full path of a running script? Benjamin Han Python 7 01-03-2004 07:15 PM



Advertisments