Re: split a directory string into a list

From: Duncan Booth (duncan.booth_at_invalid.invalid)
Date: 02/26/05


Date: 26 Feb 2005 11:09:22 GMT

Josef Meile wrote:

> "This should work ***reasonably*** reliably on Windows and Unix". Are
> there any cases when it does not work?

The most obvious case where it wouldn't work would be for a UNC path name.
Using the string split method gives two empty strings:

>>> os.path.normpath(r'\\machine\share').split(os.path.sep)
['', '', 'machine', 'share']
>>>

whereas the splitpath function I proposed gives you:

>>> splitpath(r'\\machine\share')
['\\\\', 'machine', 'share']

So to find out the type of path (relative, absolute, unc), you only have to
consider the first element with my function but you have to look at the
first two elements if you just naively split the string.

Also a relative windows path with a drive letter doesn't get fully split:

>>> os.path.normpath(r'c:dir\file').split(os.path.sep)
['c:dir', 'file']
>>> splitpath(r'c:dir\file')
['c:', 'dir', 'file']

If you really are worried about speed (and are sure you aren't optimising
prematurely), then you could combine some special case processing near the
start of the string with a simple split of the remainder.



Relevant Pages

  • RE: Issue with UNC Path (I think). Trying to build Dynamic Report
    ... Dim strBroker As String ... You may just have to determine the UNC path, ... Table and then suck in all the data from my Excel sheet on the network drive. ...
    (microsoft.public.access.formscoding)
  • Re: Get shared folders real path
    ... "WNetGetConnectionA" (ByVal lpszLocalName As String, ... Dim lpszRemoteName As String ... ' retrieves the UNC path. ...
    (microsoft.public.word.vba.general)
  • Re: split a directory string into a list
    ... > The most obvious case where it wouldn't work would be for a UNC path name. ... > consider the first element with my function but you have to look at the ... > first two elements if you just naively split the string. ... > Also a relative windows path with a drive letter doesn't get fully split: ...
    (comp.lang.python)
  • Why cant I get the UNC?
    ... I'm trying to get the UNC path of a mapped network folder, ... Imports Scripting.FileSystemObjectClass ... Public Function GetUNCFromMappedDrive(ByVal driveLetter As String) As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: SYS(?)
    ... This api doesn't work with UNC path ex: ... STRING @ lpVolumeNameBuffer,; ...
    (microsoft.public.fox.programmer.exchange)