FilePathsBase.jl icon indicating copy to clipboard operation
FilePathsBase.jl copied to clipboard

Convert Windows path to Posix path

Open aminya opened this issue 5 years ago • 5 comments

How can we convert the Windows path to the Posix path?

aminya avatar Apr 23 '20 02:04 aminya

I wrote this:

Base.convert(PosixPath, x::WindowsPath) = PosixPath(repr(x)[3:end-1])
PosixPath(x::WindowsPath) = Base.convert(PosixPath, x::WindowsPath)

Not the best method but works.

aminya avatar Apr 23 '20 02:04 aminya

Nicer way 🚀 : Anyone who is working with paths, use this function to make your path good.

using FilePathsBase
GoodPath(inp::String) = inp |> Path |> _GoodPath |> string
_GoodPath(path::WindowsPath) = PosixPath((path.drive, path.segments...))
_GoodPath(path) = path

GoodPath makes a path good. 🙅‍♂️ The result of this can be passed to all of the julia functions like cd on any platform, so it will free you up from being worried about the platform. I use GoodPath on my functions that accept a path from the user, or on the output of the functions that return a path.

julia> GoodPath("C:\\folder1\\folder2") 
"C:/folder1/folder2"

julia> GoodPath("folder1/foldee2")
"folder1/foldee2"

aminya avatar Apr 24 '20 01:04 aminya

Hmmm, might need more context on your use case. Why are you wanting to convert windows to posix paths? My first instinct is that we could support a convert method if I understood the need better. Also, why are you bothering with FilePaths if you just want a string anyways? It'd probably be faster to just do the string manipulation yourself... if that's what you want.

NOTE: Your example could be simplified to a call to replace w/o using FilePaths.

rofinn avatar Apr 24 '20 03:04 rofinn

Hmmm, might need more context on your use case. Why are you wanting to convert windows to posix paths? My first instinct is that we could support a convert method if I understood the need better. Also, why are you bothering with FilePaths if you just want a string anyways? It'd probably be faster to just do the string manipulation yourself... if that's what you want.

To write generic code that works on all platform I need a single method of writing paths. I chose the Posix method since Julia works well with it.

I used FilePathsBase methods to make a path from a string, and then dispatch on it based on the types that it detects, and then convert it back to string for my usage.

I do a lot of string interpolation, printing, etc, so I can't use FilePaths for that, and I should use strings.

""""
a lot of non path stuff

"$mygoodpath/somefolder"
a lot of non path stuff
"""

The benefit of using FilePathsBase is its path detection regardless of what is given inside:

julia> using FilePathsBase

julia> p = Path("C:\\folder1/folder2")
p"C:/folder1/folder2"

julia> typeof(p)
WindowsPath

NOTE: Your example could be simplified to a call to replace w/o using FilePaths.

Yeah probably. I am not concerned with speed here. IMO, this sentence questions the existence/implementation of FilePaths and FilePathsBase itself.

aminya avatar Apr 24 '20 04:04 aminya

I'll note that your issue with interpolation wasn't clarified here, but did come up on discourse. It isn't that you need to convert Windows to Posix paths, but rather that you want WindowsPath("C:\\Users\\Documents") to be interpolated as "C:/Users/Documents" (similar to what happens on the REPL)?

The benefit of using FilePathsBase is its path detection regardless of what is given inside

That's just one of many benefits, but if you like that feature then I'm glad it helps.

Yeah probably. I am not concerned with speed here. IMO, this sentence questions the existence/implementation of FilePaths and FilePathsBase itself.

How so? It's about using the right tool for the job.

NOTE: Please don't continue posting to both the issue and discourse as it increases the likelihood that I'll miss something in your issue/request.

rofinn avatar Apr 24 '20 16:04 rofinn