# Determine if a directory or file is writable

**URL:** <https://discourse.julialang.org/t/determine-if-a-directory-or-file-is-writable/38843>\
**Category:** General Usage\
**Tags:** filesystem\
**Created:** [May 5, 2020, 10:55pm UTC](https://discourse.julialang.org/t/determine-if-a-directory-or-file-is-writable/38843 "2020-05-05T22:55:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [May 5, 2020, 10:55pm UTC](https://discourse.julialang.org/t/determine-if-a-directory-or-file-is-writable/38843/1 "2020-05-05T22:55:38Z")

</div>

Ultimately what I would like to do is determine if a directory or file is writable by the current user. Looking at the Filesystem commands there is `uperm()`, `gperm()`, and `operm()` which at first blush look like exactly what I need. However to determine if `uperm()` is **me** I need to know my user ID so that I can compare it with the owner ID of the file. Likewise with `gperm()` I would need to know if one of **my** groups match the group of the file system object.

I haven’t been able to find a function in Julia that returns the user’s ID…nor anything about the groups the user belongs to. I would like functions that work in both Linux and Windows so I would prefer something in Julia (or a package) rather than executing external commands.

I would like to do this before I start doing some expensive operations so that I know I can save the results. And yes, with the behavior of this application it is entirely possible the user pointed me at a directory/file they do not have write access to. So it is a situation I need to handle.

Alternate solutions for testing the directory I’ve come up with, is first I could try to create an empty file in the directory, if that works, then I have write access. Or, and I like this better, I believe I could just `touch()` the directory, if that works, we should be good to go from a directory standpoint.

The file solution is trickier. I could use `touch()` or even just try to open the file for write access. But both of those would update the last modified time. And I don’t want to update the last modified time before I actually have the data to modify the file with.

Anyone have any suggestions?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 6, 2020, 7:24am UTC](https://discourse.julialang.org/t/determine-if-a-directory-or-file-is-writable/38843/2 "2020-05-06T07:24:22Z")

</div>

A very hacky (and not very portable) solution is getting the user id with something like

```julia
parse(Int, chomp(read(`id -u`, String)))

```

and then comparing it to the relevant parts from `stat(directory)` etc.

`id` can also deliver groups in a format you can parse. There is probably a neater solution, using the `getuid` etc functions from C would be more elegant.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [May 6, 2020, 11:07am UTC](https://discourse.julialang.org/t/determine-if-a-directory-or-file-is-writable/38843/3 "2020-05-06T11:07:19Z")

</div>

Thank you, that pushed me down some other pathes. 🙂 I think in the end I’m going to go for a solution that works “most” of the time. I’ll do a `touch()` on the directory if that works, then I’ll assume I have write access to the files inside. The chances of that assumption being wrong _should_ be low enough to not be a big deal. 🙂

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 6, 2020, 11:24am UTC](https://discourse.julialang.org/t/determine-if-a-directory-or-file-is-writable/38843/4 "2020-05-06T11:24:42Z")

</div>

Another solution that occurs to me is

```julia
is_writable(path) = success(`test -w $(path)`)

```

which should work on Linux, don’t know about OS X.

It would be great if at least a package could expose `getuid` & friends.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [May 6, 2020, 11:26am UTC](https://discourse.julialang.org/t/determine-if-a-directory-or-file-is-writable/38843/5 "2020-05-06T11:26:15Z")

</div>

Oh, I like that. 🙂

---

<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:** [May 6, 2020, 11:45am UTC](https://discourse.julialang.org/t/determine-if-a-directory-or-file-is-writable/38843/6 "2020-05-06T11:45:43Z")

</div>

> [@Tamas\_Papp](#):
>
> It would be great if at least a package could expose `getuid` & friends.

If you want to call `getuid`, you can just do `ccall(:getuid, Cint, ())`.

However, neither this nor any of the above solutions will work on Windows, where [checking write permissions seems incredibly complicated](http://blog.aaronballman.com/2011/08/how-to-check-access-rights/) if you don’t want to try writing to the file.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 6, 2020, 11:55am UTC](https://discourse.julialang.org/t/determine-if-a-directory-or-file-is-writable/38843/7 "2020-05-06T11:55:20Z")

</div>

To be fair, a lot of things can complicate the issue on Linux too (eg SELinux).

Perhaps a `try ... catch` block would work best, aiming to actually perform whatever task is needed, catching an `IOError` for the “permission denied” code, and rethrowing everything else. Exceptions are _relatively_ costly, but not in the context of filesystem access.
