Python

2013-02-22 8 views
33

'daki mutlak dosya yolunun dizin yolunu alın. Dosyanın bulunduğu dizini almak istiyorum. Örneğin tam yol:Python

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

böyle yapabileceğini:

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

Ama Yukarıdaki örnekte belirli dizin ayırıcı dayanır ve gerçekten hoş değil. Daha iyi bir yolu var mı?

+2

http://docs.python.org/2/library/os.path.html#os.path. dizinadı –

cevap

53

Bu arıyoruz:

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

İlgili fonksiyonlar:

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