使用os.path.dirname() 函数可以轻松处理。
例子如下:
>>> import os
>>> s = 'http://melodi.ee.washington.edu/people/bilmes/mypapers/em.pdf'
>>> path = os.path.dirname(s)
>>> path
'http://melodi.ee.washington.edu/people/bilmes/mypapers'
>>> filename = s[len(path):]
>>> filename = s[len(path):]
>>> filename
'/em.pdf'
>>> filename = s[len(path)+1:]
>>> filename
'em.pdf'
>>> f = 'F:/OpenSource/tinyxml_2_6_2/tinyxml/xmltest.cpp'
>>> pathf = os.path.dirname(f)
>>> pathf
'F:/OpenSource/tinyxml_2_6_2/tinyxml'
>>> fileName = f[len(pathf)+1:]
>>> fileName
'xmltest.cpp'
>>>