Deniz Dogan
Deniz Dogan

Reputation: 26227

Path to current file depends on how I execute the program

This is my Python program:

#!/usr/bin/env python

import os

BASE_PATH = os.path.dirname(__file__)
print BASE_PATH

If I run this using python myfile.py it prints an empty string. If I run it using myfile.py, it prints the correct path. Why is this? I'm using Windows Vista and Python 2.6.2.

Upvotes: 5

Views: 1809

Answers (3)

KamilD
KamilD

Reputation: 163

In many cases it's better to use:

os.path.dirname(sys.argv[0])

Upvotes: 0

tommy
tommy

Reputation: 1

os.path.normpath(os.path.join(os.getcwd(),os.path.dirname(__file__)))

Upvotes: 0

Alex Martelli
Alex Martelli

Reputation: 882631

It's just a harmless windows quirk; you can compensate by using os.path.abspath(__file__), see the docs

Upvotes: 8

Related Questions