Azimout's Linux weblog

Python script’s path

leave a comment »

What won’t work:

  • os.getcwd() is the directory you’re currently in. If you’re in the same directory as the script, and you just ran “python”, this will work. But if you’re e.g. in your home dir, and from there you ran “python /absolute/path/to/script/”, then os.getcwd() will return your home dir, not the script’s path”
  • os.path.dirname(__file__) will work in the opposite case than above, since __file__ is the script name you called on the command line.
  • sys.argv[0], since it’s the same as __file__

What does work: sys.path[0]


Written by azimout

03/05/2011 at 08:29

Posted in Tricks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: