Filepath string manipulation help

Fredrik Lundh fredrik at pythonware.com
Wed Nov 2 14:41:01 EST 2005


"mjakowlew"wrote:

> filepath='c:\documents\web\zope\file.ext'
>
> I need to extract everthing after the last '\' and save it.

that string doesn't contain what you think it does:

>>> filepath='c:\documents\web\zope\file.ext'
>>> filepath
'c:\\documents\\web\\zope\x0cile.ext'
>>> print filepath
c:\documents\web\zope?ile.ext

if you fix that, you can use os.path.basename() to strip off the last
part:

>>> filepath=r'c:\documents\web\zope\file.ext'
>>> filepath
'c:\\documents\\web\\zope\\file.ext'
>>> print filepath
c:\documents\web\zope\file.ext

>>> import os
>>> os.path.basename(filepath)
'file.ext'

for more info on Python's string literal syntax, see section 3.1.2 in the
Python Tutorial, and this page:

    http://docs.python.org/ref/strings.html

</F> 






More information about the Python-list mailing list