2013-02-22 28 views
33

में पूर्ण फ़ाइल पथ का निर्देशिका पथ प्राप्त करें मैं उस निर्देशिका को प्राप्त करना चाहता हूं जहां फ़ाइल मौजूद है। उदाहरण के लिए पूरा पथ है:पायथन

fullpath = "/absolute/path/to/file" 
# something like: 
os.getdir(fullpath) # if this existed and behaved like I wanted, it would return "/absolute/path/to" 

मैं इसे इस तरह कर सकता है:

dir = '/'.join(fullpath.split('/')[:-1]) 

लेकिन उपरोक्त उदाहरण विशिष्ट निर्देशिका विभाजक पर निर्भर करता है और वास्तव में सुंदर नहीं है। क्या कोई बेहतर तरीका है?

+2

http://docs.python.org/2/library/os.path.html#os.path। dirname –

उत्तर

53

आप इस के लिए देख रहे हैं:

>>> import os.path 
>>> fullpath = '/absolute/path/to/file' 
>>> os.path.dirname(fullpath) 
'/absolute/path/to' 

संबंधित कार्य:

>>> os.path.basename(fullpath) 
'file' 
>>> os.path.split(fullpath) 
('/absolute/path/to','file') 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^