views:

46

answers:

3

I want get the full directory path of current file. I tried

os.path.abspath(__file__)

But it return the full path including the file.

C:\\python27\\test.py

Where as I just need

C:\\python27\\
A: 

try this

import os
print os.getcwd()
org.life.java
Or perform some string operation to get the required output.
org.life.java
Well I think thats a complete different thing. Say if you call `test.py` from D:\ drive instead of returning `C:\\python27` it will return `D:\\`
Shubham
ok I think misunderstood your question
org.life.java
+3  A: 

If you mean the directory of the script being run:

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

Though, calling abspath isn't necessary since dirname will give you a proper directory name.

If you mean the current working directory:

import os
os.getcwd()
Bryan Oakley
+2  A: 
import os
print os.path.dirname(__file__)
chefsmart