# Can Julia always return the path to a directory with \`/\` at the end?

**URL:** <https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974>\
**Category:** General Usage\
**Tags:** question, filesystem\
**Created:** [April 4, 2022, 2:07am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974 "2022-04-04T02:07:13Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [April 4, 2022, 2:07am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/1 "2022-04-04T02:07:13Z")

</div>

In Julia, sometimes a path to a directory is returned with `/` at the end, but sometimes not. For example,

```julia-repl
julia> homedir()
"/users/xxxxx"

julia> joinpath(homedir(), "documents/")
"/users/xxxxx/documents/"

```

Here `/users/xxxxx/documents/` is a directory, so is `/users/xxxxx`. But in the first line, it returns the result without the final `/`, while in the second line, it does. How can we unify this behavior? Can Julia always return the path, as long as it is a directory, with `/`; while if it is a file, without `/`?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 4, 2022, 2:11am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/2 "2022-04-04T02:11:00Z")

</div>

No, please no. Some Linux tools like `rsync`, have different behavior depending on if the final slash is or not provided, this could introduce subtle intercommunication bugs.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [April 4, 2022, 2:14am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/3 "2022-04-04T02:14:46Z")

</div>

OK, if so, can we have an `issamepath` function in `Base`? Because now, all paths are just strings, so if I want to compare them, `"/users/xxxxx/documents/"` is not equal to `"/users/xxxxx/documents"`, and that means they are different paths. This is not what I want.  
I know we have [FilePathsBase.jl](https://github.com/rofinn/FilePathsBase.jl), but it would be really nice if this is implemented in `Base`.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 4, 2022, 3:05am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/5 "2022-04-04T03:05:18Z")

</div>

What is your criteria for two paths being the same? Or better yet, does your definition of `path` includes single files? If `documents` is a file then `"/users/xxxxx/documents"` is a correct way to refer to it, and `"/users/xxxxx/documents/"` is an incorrect way.

I find funny that `homedir()` returns `"/users/xxxxx"` because this means `isdirpath(homedir()) === false`, Julia function `isdirpath` just checks if the path ends with a separator, it does not looks at the filesystem. Sincerely, it kinda seems to me that the `Base` filesystem utilities are kinda bad designed. But, perhaps, the problem is that there is a set of functions that avoid looking into the filesystem and another that use the information there to make inferences.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [April 4, 2022, 3:05am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/6 "2022-04-04T03:05:27Z")

</div>

> There is `abspath(a) == abspath(b)`

It does not work.

```julia
julia> abspath("/users/xxxxx/documents/") == abspath("/users/xxxxx/documents")
false

```

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [April 4, 2022, 3:11am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/7 "2022-04-04T03:11:21Z")

</div>

> What is your criteria for two paths being the same?

My criterion is: if a path refers to a file, it should be ended without `/`; if it is a directory, it should be ended with `/`. So, if we want to check if `"/users/xxxxx/documents/"` and `"/users/xxxxx/documents"` are equal, we should check whether they are both directories, not just string comparison.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [April 4, 2022, 3:13am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/8 "2022-04-04T03:13:01Z")

</div>

> Sincerely, it kinda seems to me that the `Base` filesystem utilities are kinda bad designed.

We really should have an object-based (rather than string-based) path library in the standard libraries, like `pathlib` in Python.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 4, 2022, 3:19am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/9 "2022-04-04T03:19:31Z")

</div>

As you can see, standard library tools often end up being reconsidered but they cannot be improved or replaced, saddling the ecosystem with baggage I’d rather do without. `using FilePathsBase` is quite easy to type, especially if added to `setup.jl`.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [April 4, 2022, 3:21am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/10 "2022-04-04T03:21:12Z")

</div>

> [@jar1](#):
>
> standard library tools often end up being reconsidered but they cannot be improved or replaced, saddling the ecosystem with baggage I’d rather do without

I understand. I am just saying the current string-based path system was poorly designed and did not take inspiration from `pathlib`. I wish we can have that in Julia 2.0.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [April 4, 2022, 5:31am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/11 "2022-04-04T05:31:05Z")

</div>

> [@singularitti](#):
>
> It does not work.
> 
> ```julia
> julia> abspath("/users/xxxxx/documents/") == abspath("/users/xxxxx/documents")
> false
> 
> ```

This does work though:

```julia
julia> realpath("/tmp") == realpath("/tmp/")
true

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 4, 2022, 5:57am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/12 "2022-04-04T05:57:40Z")

</div>

> [@singularitti](#):
>
> My criterion is: if a path refers to a file, it should be ended without `/` ; if it is a directory, it should be ended with `/` .

So, should it be filesystem-aware? Or can it refer to things that do not exist (yet)? It is just a question of representation (all folder representations should always end with a `/`) or is it a question of matching what it is in the filesystem?

I ask because: if you let someone input a path to you, either they need to be aware of this representation detail, i.e., if the path refers to a folder, they are obligated to give this information by leaving a trailing slash; and it raises the question, what if they do not know if it is a folder beforehand, or do want the code to work generically for both alternatives? If it is not a matter of representation, the other problem is that the file/folder may not yet exist, and looking at the filesystem also does not give an answer.

> [@GunnarFarneback](#):
>
> ```julia
> julia> realpath("/tmp") == realpath("/tmp/")
> true
> 
> ```

A possible problem with this alternative is that if one of the paths describes something that do not exist, then the function call throws an error.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 4, 2022, 6:50am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/13 "2022-04-04T06:50:10Z")

</div>

On windows, paths are separated with backslash but on unix you can make a regular file `\`

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [April 4, 2022, 4:00pm UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/14 "2022-04-04T16:00:51Z")

</div>

joinpath returns whatever you supply in the filename yourself.

You just happen to tell it to append “documents/” to homedir().

```julia
julia> joinpath(homedir(), "documents")
"/home/matt/documents"

julia> joinpath(homedir(), "documents\\")
"/home/matt/documents\\"

julia> joinpath(homedir(), "documents/")
"/home/matt/documents/"

```

Julia does not check if `documents` is an existing directory so cannot reliably append a directory separator.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [April 5, 2022, 6:14am UTC](https://discourse.julialang.org/t/can-julia-always-return-the-path-to-a-directory-with-at-the-end/78974/15 "2022-04-05T06:14:45Z")

</div>

On your wider question of making the path dependent on the file system properties of the members of said path.

1. For some filesystems folders can also be read like files.
2. In distributed systems, file paths can be built without access to the file system
3. mkpath(“/this/does/not/exist/yet”)
4. `stat` is expensive
5. `stat` is even more expensive when it is a distributed file system, it might be going to Mars and back!
6. None of these things stop you using a filesystem aware module.

The Julia way is to have as few items in Base as is reasonable and be `using` anything else.

Personally I cannot see the utility. Obviously you do.

I think invoking a system call to the kernel to create a representation of a path is too much overhead and is something that should be under control of the programmer.
