2012-09-13 12 views
3

我想編寫一個Python功能是能夠把文件路徑,如:Python函數提取一個文件路徑的多段

/ABS /路徑/要/我的/文件/文件.TXT

和返回三個弦變量:

  • /abs - 根目錄下,再加上 「最頂端的」 路徑目錄
  • file - 路徑中的「最底部」目錄;的file.txt
  • path/to/my父 - 一切都在路徑的頂部和底部最目錄之間

因此就用下面的僞代碼:提前

def extract_path_segments(file): 
    absPath = get_abs_path(file) 
    top = substring(absPath, 0, str_post(absPath, "/", FIRST)) 
    bottom = substring(absPath, 0, str_post(absPath, "/", LAST)) 
    middle = str_diff(absPath, top, bottom) 

    return (top, middle, bottom) 

感謝您的任何幫助這裏!

回答

5

您正在查找os.sep,連同各種os.path模塊功能。只需按該字符分割路徑,然後重新組裝要使用的部分。喜歡的東西:

import os 

def extract_path_segments(path, sep=os.sep): 
    path, filename = os.path.split(os.path.abspath(path)) 
    bottom, rest = path[1:].split(sep, 1) 
    bottom = sep + bottom 
    middle, top = os.path.split(rest) 
    return (bottom, middle, top) 

這並不不是處理得很好,將Windows路徑,其中兩個\/是合法的路徑分隔符。在這種情況下,你的也有有一個驅動器號,所以你不得不特殊情況。

輸出:

>>> extract_path_segments('/abs/path/to/my/file/file.txt') 
('/abs', 'path/to/my', 'file') 
+0

無法在Windows上使用'「或''/」'作爲路徑分隔符嗎?我不認爲這會在這種情況下工作...(雖然,我願意不在Windows上工作;-) – mgilson

+0

@mgilson:是的,windows同時接受,但'os.sep'總是'\ \'。這很痛苦,所以我將'sep'定義爲一個默認爲'os.sep'的關鍵字參數。 –

+0

Quoth ['os.sep'文檔](http://docs.python.org/library/os.html#os.sep):「*請注意,知道這不足以解析或連接路徑名 - 使用os.path.split()和os.path.join() - 但偶爾會有用。*「 –

3

使用os.path.split

import os.path 

def split_path(path): 
    """ 
    Returns a 2-tuple of the form `root, list_of_path_parts` 
    """ 
    head,tail = os.path.split(path) 
    out = [] 
    while tail: 
     out.append(tail) 
     head,tail = os.path.split(head) 
    return head,list(reversed(out)) 

def get_parts(path): 
    root,path_parts = split_path(path) 
    head = os.path.join(root,path_parts[0]) 
    path_to = os.path.join(*path_parts[1:-2]) 
    parentdir = path_parts[-2] 
    return head,path_to,parentdir 

head,path_to,parentdir = get_parts('/foo/path/to/bar/baz') 
print (head)  #foo 
print (path_to)  #path/to 
print (parentdir) #bar 
+0

符合文檔中的推薦?檢查。跨平臺?檢查。這是做到這一點的方法。應該注意的是,'get_parts'需要絕對路徑和規範化路徑作爲參數(例如'/ abs/path/to/my/file/subdir /../ file.txt'將失敗),並且它不能處理windows網絡掛載('\\ server \ share \ abs \ path \ to \ my \ file \ file.txt'),但這些問題[很容易](http://docs.python.org/library/os.path.html #os.path.abspath)[修改](http://docs.python.org/library/os.path.html#os.path.splitunc)。 –

2

使用os.path.split()os.path.join(),我們都應該

>>> import os 
>>> pth = "/abs/path/to/my/file/file.txt" 
>>> parts = [] 
>>> while True: 
...  pth, last = os.path.split(pth) 
...  if not last: 
...   break 
...  parts.append(last) 
... 
>>> pth + parts[-1] 
'/abs' 
>>> parts[1] 
'file' 
>>> os.path.join(*parts[-2:1:-1]) 
'path/to/my' 

其功能

import os 

def extract_path_segments(pth): 
    parts = [] 
    while True: 
     pth, last = os.path.split(pth) 
     if not last: 
      break 
     parts.append(last) 
    return pth + parts[-1], parts[1], os.path.join(*parts[-2:1:-1]) 
1
>>> p = '/abs/path/to/my/file/file.txt' 
>>> r = p.split('/') 
>>> r[1],'/'.join(r[2:-2]),r[-2] 
('abs', 'path/to/my', 'file')