How to read the directory which the actively running python file islocated in?

Discussion in 'Python' started by Michael Malinowski, Dec 1, 2006.

  1. Is there a way to read the directory that the currently running python file
    is located in?
    Cheers
    Mike.
     
    Michael Malinowski, Dec 1, 2006
    #1
    1. Advertising

  2. Michael Malinowski

    anders Guest

    Re: How to read the directory which the actively running python file is located in?

    in os module there is many funktion/methods to extract this information
    to ask the path to the current running pythonprogram you can do likes
    this

    ----- CUT-----------
    import os
    print os.getcwd()

    ----- CUT ------

    // Anders


    Michael Malinowski skrev:

    > Is there a way to read the directory that the currently running python file
    > is located in?
    > Cheers
    > Mike.
     
    anders, Dec 1, 2006
    #2
    1. Advertising

  3. Michael Malinowski

    anders Guest

    Re: How to read the directory which the actively running python file is located in?

    in os module there is many funktion/methods to extract this information
    to ask the path to the current running pythonprogram you can do likes
    this

    ----- CUT-----------
    import os
    print os.getcwd()

    ----- CUT ------

    // Anders


    Michael Malinowski skrev:

    > Is there a way to read the directory that the currently running python file
    > is located in?
    > Cheers
    > Mike.
     
    anders, Dec 1, 2006
    #3
  4. Michael Malinowski

    Gerold Penz Guest

    Re: How to read the directory which the actively running python fileis located in?

    Michael Malinowski schrieb:
    > Is there a way to read the directory that the currently running python file
    > is located in?


    Hi Mike!

    To get the started program:

    sys.argv[0]

    DonĀ“t use ``os.curdir``.


    To get the filename, of the current module:

    __file__


    To get the directory:

    os.path.split(sys.argv[0])[0]
    os.path.split(__file__)[0]


    Regards,
    Gerold
    :)

    --
    ________________________________________________________________________
    Gerold Penz - bcom - Programmierung
    | http://gerold.bcom.at | http://sw3.at
    Ehrliche, herzliche Begeisterung ist einer der
    wirksamsten Erfolgsfaktoren. Dale Carnegie
     
    Gerold Penz, Dec 1, 2006
    #4
  5. Michael Malinowski

    John Machin Guest

    Re: How to read the directory which the actively running python file is located in?

    anders wrote:
    > in os module there is many funktion/methods to extract this information
    > to ask the path to the current running pythonprogram you can do likes
    > this
    >
    > ----- CUT-----------
    > import os
    > print os.getcwd()
    >
    > ----- CUT ------
    >
    > // Anders
    >
    >
    > Michael Malinowski skrev:
    >
    > > Is there a way to read the directory that the currently running python file
    > > is located in?
    > > Cheers
    > > Mike.


    os.getcwd() provides the "current working directory". This is *not*
    necessarily the directory that the "currently running python file"
    [whatever that means] is located in.

    Try picking what you really want/need out of this:

    C:\junk\foo>type ..\where.py
    import sys, os
    if __name__ == "__main__":
    print "running as script"
    else:
    print "imported module named", __name__
    print "code loaded from file", __file__
    print "sys.argv[0] is", sys.argv[0]
    print "cwd is", os.getcwd()

    C:\junk\foo>..\where.py
    running as script
    code loaded from file C:\junk\where.py
    sys.argv[0] is C:\junk\where.py
    cwd is C:\junk\foo

    C:\junk\foo>type runwhere.py
    import sys
    sys.path[0:0] = ['c:\\junk']
    import where

    C:\junk\foo>runwhere.py
    imported module named where
    code loaded from file c:\junk\where.py
    sys.argv[0] is C:\junk\foo\runwhere.py
    cwd is C:\junk\foo

    C:\junk\foo>cd ..

    C:\junk>md bar

    C:\junk>cd bar

    C:\junk\bar>..\foo\runwhere.py
    imported module named where
    code loaded from file c:\junk\where.pyc
    sys.argv[0] is C:\junk\foo\runwhere.py
    cwd is C:\junk\bar

    HTH,
    John
     
    John Machin, Dec 1, 2006
    #5
    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. gaurav tyagi
    Replies:
    0
    Views:
    5,025
    gaurav tyagi
    Mar 18, 2006
  2. Gordon Beaton
    Replies:
    2
    Views:
    5,101
    Bryan Castillo
    Jul 8, 2003
  3. Replies:
    9
    Views:
    62,074
    ytterbius
    May 10, 2007
  4. Replies:
    8
    Views:
    238
    Swanagon, John M \(John\)
    Jul 25, 2006
  5. Roberto Gallo

    Actively call a JavaScript from a Applet.

    Roberto Gallo, Feb 20, 2004, in forum: Javascript
    Replies:
    3
    Views:
    87
    Laurent Bugnion
    Feb 22, 2004
Loading...

Share This Page