Parsing a path to components

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Jun 7 05:34:53 EDT 2008


On Sat, 07 Jun 2008 02:15:07 -0700, s0suk3 wrote:

> On Jun 7, 3:15 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
>> On Fri, 06 Jun 2008 23:57:03 -0700, s0suk3 wrote:
>> > You can just split the path on `os.sep', which contains the path
>> > separator of the platform on which Python is running:
>>
>> > components = pathString.split(os.sep)
>>
>> Won't work for platforms with more than one path separator and if a
>> separator is repeated.  For example r'\foo\\bar/baz//spam.py' or:
>>
>> In [140]: os.path.split('foo//bar')
>> Out[140]: ('foo', 'bar')
>>
>> In [141]: 'foo//bar'.split(os.sep)
>> Out[141]: ['foo', '', 'bar']
>>
> 
> But those are invalid paths, aren't they?

No.  See `os.altsep` on Windows.  And repeating separators is allowed too.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list