splitting string

Matthias Huening mhuening at zedat.fu-berlin.de
Thu Jul 8 06:31:58 EDT 2004


"Laura McCord" <Laura.McCord at doucet-austin.com> wrote in 
news:mailman.98.1089276242.5135.python-list at python.org:

> I want to split a filename string such as Y:\folder\directory\file 
> 
> it is stored like this in my python code:
>  fname = self.filename
> 
> How can I get rid of everything before the word 'file'?

Try this:

>>> import os.path
>>> x = os.path.split('Y:\\folder\\directory\\file')
>>> x
('Y:\\folder\\directory', 'file')
>>> x[1]
'file'

You'll need to escape the slashes. 
Alternatively you could use r'...':
os.path.split(r'Y:\folder\directory\file')
or
os.path.split('Y:/folder/directory/file')

Matthias



More information about the Python-list mailing list