2014-06-15 44 views
0

構建在Python當我打印與os.join目錄路徑構成的路徑名unecssary目錄我得到的是這樣的:刪除與os.join

rep/rep2/../rep1 

有沒有辦法讓這個只有:

rep/rep1 

回答

1

使用os.path.relpath

>>> import os 
>>> os.path.relpath("rep/rep2/../rep1", start="") 
'rep/rep1' 

或者os.path.normpath

>>> import os 
>>> os.path.normpath("rep/rep2/../rep1") 
'rep/rep1'