nemec / pathlib

Path manipulation library for .Net
MIT License
63 stars 9 forks source link

Posix path using wrong separator #21

Open MattAlanWright opened 3 weeks ago

MattAlanWright commented 3 weeks ago

The code snippet below:

class Program
{
    static void Main(string[] args)
    {
        const string stringPath = @"/this/is/a/file\named\this.txt";
        PurePosixPath posixPath = new(stringPath);
        Console.WriteLine(posixPath.Directory);
        Console.WriteLine(posixPath.Filename);
    }
}

prints the following when run on my Windows machine:

/this/is/a/file/named
this.txt

Clearly it is interpreting the '\' characters as path separators. However, these are valid filename characters under Posix and I would expect the following output:

/this/is/a
file\named\this.txt

I assume this is related to running on Windows. However my understanding was that the Pure path types don't care what machine they are being run on. Being able to easily manipulate either Windows or Posix paths from a Windows machine, is what initially drew me to this library.

MattAlanWright commented 2 weeks ago

For more context, here is what the same attempt looks like in Python using pathlib, still running on Windows:

>>> import pathlib
>>> ppp = pathlib.PurePosixPath("/this/is/a/file\named\thing.txt") 
>>> ppp.name
'file\named\thing.txt'
>>> ppp.parent
PurePosixPath('/this/is/a')
>>> ppp.parts
('/', 'this', 'is', 'a', 'file\named\thing.txt')