Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Windows path to Posix path #87

Open
aminya opened this issue Apr 23, 2020 · 5 comments · May be fixed by #88
Open

Convert Windows path to Posix path #87

aminya opened this issue Apr 23, 2020 · 5 comments · May be fixed by #88

Comments

@aminya
Copy link

aminya commented Apr 23, 2020

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

@aminya

This comment has been minimized.

@aminya
Copy link
Author

aminya commented Apr 24, 2020

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"

@rofinn
Copy link
Owner

rofinn commented Apr 24, 2020

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.

@aminya
Copy link
Author

aminya commented Apr 24, 2020

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.

@rofinn
Copy link
Owner

rofinn commented Apr 24, 2020

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 rofinn transferred this issue from rofinn/FilePaths.jl Apr 27, 2020
@rofinn rofinn linked a pull request Apr 27, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants