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

Discussion in 'Python' started by gmax2006, Aug 29, 2006.

  1. gmax2006

    gmax2006 Guest

    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
    gmax2006, Aug 29, 2006
    #1
    1. Advertising

  2. gmax2006

    Miki Guest

    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
    Miki, Aug 29, 2006
    #2
    1. Advertising

  3. gmax2006

    Eyal Lotem Guest

    >> 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.
    Eyal Lotem, Aug 29, 2006
    #3
  4. gmax2006

    Ivan Zuzak Guest

    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
    Ivan Zuzak, Aug 30, 2006
    #4
  5. gmax2006

    Joel Hedlund Guest

    >> 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
    Joel Hedlund, Aug 31, 2006
    #5
  6. gmax2006

    Joel Hedlund Guest

    > 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
    Joel Hedlund, Aug 31, 2006
    #6
  7. gmax2006

    Ivan Zuzak Guest

    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
    Ivan Zuzak, Aug 31, 2006
    #7
  8. gmax2006

    John Machin Guest

    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".
    John Machin, Aug 31, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    885
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  2. Benjamin Han
    Replies:
    7
    Views:
    486
    Hartmut Goebel
    Jan 3, 2004
  3. Matt
    Replies:
    3
    Views:
    3,292
    Lawrence D'Oliveiro
    Nov 7, 2010
  4. happykid
    Replies:
    6
    Views:
    629
    WaffleSouffle
    Aug 9, 2011
  5. sm
    Replies:
    3
    Views:
    102
    Tad McClellan
    Jan 26, 2006
Loading...

Share This Page