# Convert filepath to URI

**URL:** https://discourse.julialang.org/t/convert-filepath-to-uri/123334
**Category:** New to Julia
**Created:** [December 2, 2024, 12:54am UTC](https://discourse.julialang.org/t/convert-filepath-to-uri/123334 "2024-12-02T00:54:16Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![thestoicone](https://avatars.discourse-cdn.com/v4/letter/t/c5a1d2/32.png) [@thestoicone](https://discourse.julialang.org/u/thestoicone)
#### Post date: [December 2, 2024, 12:54am UTC](https://discourse.julialang.org/t/convert-filepath-to-uri/123334/1 "2024-12-02T00:54:16Z")

</div>

I’ve been knocking my head against a wall trying to fix an issue in the .xspf (VLC) playlist generator I’m writing. I used “walkdir” to get the paths of the files but I can’t seem to convert them into the correct format (HTML/ANSI URL/URI?) that the .xspf file needs. I am working in Windows.

For instance, one of my entries says:  
file:///C:\Music\Adiemus\Adiemus IV\_ The Eternal Knot\04 The Wooing Of Etain.mp3

When it should say:  
file:///C:/Music/Adiemus/Adiemus%20IV\_%20The%20Eternal%20Knot/04%20The%20Wooing%20Of%20Etain.mp3

Can anyone tell me how to fix this?

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [December 2, 2024, 1:32am UTC](https://discourse.julialang.org/t/convert-filepath-to-uri/123334/2 "2024-12-02T01:32:00Z")

</div>

The following does the job for the strings in the post:

```julia
julia> s = "file:///C:\\Music\\Adiemus\\Adiemus IV_ The Eternal Knot\\04 The Wooing Of Etain.mp3"
"file:///C:\\Music\\Adiemus\\Adiemus IV_ The Eternal Knot\\04 The Wooing Of Etain.mp3"

julia> println(s)
file:///C:\Music\Adiemus\Adiemus IV_ The Eternal Knot\04 The Wooing Of Etain.mp3

julia> t = replace(replace(s, ' '=>"%20"),'\\'=>"/")
"file:///C:/Music/Adiemus/Adiemus%20IV_%20The%20Eternal%20Knot/04%20The%20Wooing%20Of%20Etain.mp3"

```

But this is a brittle solution. There are many gotchas with these encodings and depending on the level of correctness and the degree to which the inputs are exposed to manipulation, there is much more care which can be taken.

---

<div class="post-metadata">

### Author: ![thestoicone](https://avatars.discourse-cdn.com/v4/letter/t/c5a1d2/32.png) [@thestoicone](https://discourse.julialang.org/u/thestoicone)
#### Post date: [December 2, 2024, 1:36am UTC](https://discourse.julialang.org/t/convert-filepath-to-uri/123334/3 "2024-12-02T01:36:36Z")

</div>

Thanks, I’ll give it a try. I didn’t know if there was package that existed that would do the job that I missed 🙂

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 2, 2024, 1:40am UTC](https://discourse.julialang.org/t/convert-filepath-to-uri/123334/4 "2024-12-02T01:40:07Z")

</div>

See [What to use for URL encoding? - #3 by quinnj](https://discourse.julialang.org/t/what-to-use-for-url-encoding/25467/3) — ~~the HTTP.jl package~~ the URIs.jl package has a function `escapeuri` to escape strings for URIs. It won’t convert backslash to forward slashes, but you can apply it to _each component_ of the path (via `splitpath`, which splits by `\` on Windows) to escape special characters like spaces. In particular, you can define the function:

```julia
import URIs
path2uri(path) = "file://" * join(map(URIs.escapeuri, splitpath(path)), '/')

```

and apply it to convert your path to a `file://` URI.

(It might be nice to have a function like this in the URIs.jl package if someone feels like ~~filing an issue or~~ making a PR for the issue: [filepath2uri function? · Issue #61 · JuliaWeb/URIs.jl · GitHub](https://github.com/JuliaWeb/URIs.jl/issues/61))

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 2, 2024, 2:58am UTC](https://discourse.julialang.org/t/convert-filepath-to-uri/123334/5 "2024-12-02T02:58:10Z")

</div>

2 posts were split to a new topic: [Splitpath/joinpath for non-native paths?](https://discourse.julialang.org/t/splitpath-joinpath-for-non-native-paths/123336)

---

<div class="post-metadata">

### Author: ![thestoicone](https://avatars.discourse-cdn.com/v4/letter/t/c5a1d2/32.png) [@thestoicone](https://discourse.julialang.org/u/thestoicone)
#### Post date: [December 2, 2024, 4:34am UTC](https://discourse.julialang.org/t/convert-filepath-to-uri/123334/6 "2024-12-02T04:34:43Z")

</div>

Thanks Dan and stevengj, I used both your methods to get it working. I used the path2uri function in which i did a replace for the Di which got mangled. It’s all working now.

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [December 2, 2024, 6:07am UTC](https://discourse.julialang.org/t/convert-filepath-to-uri/123334/7 "2024-12-02T06:07:12Z")

</div>

We have been using the functions in [JuliaWorkspaces.jl/src/URIs2/uri\_helpers.jl at main · julia-vscode/JuliaWorkspaces.jl · GitHub](https://github.com/julia-vscode/JuliaWorkspaces.jl/blob/main/src/URIs2/uri_helpers.jl) for the LanguageServer.jl for this kind of stuff for a very long time, so in a way they are very battle tested.

I wanted to move this stuff out into its own package forever, but always got stuck. [GitHub - davidanthoff/URIs2.jl](https://github.com/davidanthoff/URIs2.jl) is how far I got, should probably just finish that at some point.

---

<div class="post-metadata">

### Author: ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)
#### Post date: [December 2, 2024, 9:11am UTC](https://discourse.julialang.org/t/convert-filepath-to-uri/123334/8 "2024-12-02T09:11:28Z")

</div>

See also: [https://github.com/JuliaLang/julia/pull/55454](https://github.com/JuliaLang/julia/pull/55454) (for code copy/paste/inspiration purposes only, this is part of Base’s private API).
