filepath should not return trailing path separators

Simon Marlow simonmarhaskell at
Fri Jun 15 04:05:54 EDT 2007

Bryan O'Sullivan wrote:
> Hi, Neil -
> I'm writing some filesystem traversal code using filepath at the moment, 
> and I am a bit disappointed to find it more error-prone and cumbersome 
> than I had hoped.
> The root of my problem is that splitFileName leaves a trailing path 
> separator on the end of the first element of the tuple it returns:
>   splitFileName "foo/bar" ==> ("foo/", "bar")
> If I'm recursing up a file hierarchy towards the root directory (a 
> common thing to do), this trailing path separator is a pain.  Either I 
> forget that it's there and infinitely loop in the same directory over 
> and over again, due to this behaviour:
>   splitFileName "foo/" ==> ("foo/", "")

FWIW I think you're right here.  But I thought I'd mention the following trick I 
cooked up recently.  It meant that I didn't run into the above problem, and 
might be relevant for what you're doing:

pathParents = scanl1 (</>) . splitDirectories
   -- > scanl1 (</>) (splitDirectories "/a/b/c")
   -- ["/","/a","/a/b","/a/b/c"]


More information about the Libraries mailing list