Kamalakshi
Kamalakshi

Reputation: 7548

How to get full path of current directory in Python2.7 in Windows?

I tried some of the answers to the existing post "How to get full path of current directory in Python?". I'm using Python 2.7 on Windows. If my full path is "c:\alpha\beta\gamma", i get only "gamma" and not the full path. How should I coax python on windows not to swallow the prefix?

Upvotes: 1

Views: 2680

Answers (1)

amiller27
amiller27

Reputation: 506

This line of code should work for any file, as long as you are using Python 2.7. Using __file__ will use the current script for the file to locate.

os.path.dirname(os.path.abspath(__file__))

The abspath() part is necessary to get the full absolute path name instead of simply the directory name.

Upvotes: 2

Related Questions