string.lstrip stripping too much?

M.E.Farmer mefjr75 at hotmail.com
Mon May 16 03:23:44 EDT 2005


Roy Smith:
> I suspect what you really want to be doing is using the os.path
module.
> It's got functions for tearing pathnames apart into components, and
hides
> all the uglyness like whether / or \ is the directory separator on
your
> particular system.


I thought so too, maybe this will help.
>>> d = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona'
>>> import os
>>> os.path.split(d)
('D:\\music\\D\\Daniel Lanois', 'For the beauty of Wynona')
>>> os.path.basename(d)
'For the beauty of Wynona'
>>> os.path.dirname(d)
'D:\\music\\D\\Daniel Lanois'
>>> os.path.splitext(d)
('D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona', '')
>>> os.path.splitdrive(d)
('D:', '\\music\\D\\Daniel Lanois\\For the beauty of Wynona')
>>> os.path.split(d)
('D:\\music\\D\\Daniel Lanois', 'For the beauty of Wynona')
>>> q = os.path.split(d)
>>> q
('D:\\music\\D\\Daniel Lanois', 'For the beauty of Wynona')
>>> q = os.path.split(q[0])
>>> q
('D:\\music\\D', 'Daniel Lanois')
>>> q = os.path.split(q[0])
>>> q
('D:\\music', 'D')

And another idea might be to do a split using os.sep:
>>> import os
>>> d = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona'
>>> d.split(os.sep)
['D:', 'music', 'D', 'Daniel Lanois', 'For the beauty of Wynona']

Hth,
M.E.Farmer




More information about the Python-list mailing list