# Is there a way to find out free disk space equivalent to psutil.disk\_usage('/') in python

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-find-out-free-disk-space-equivalent-to-psutil-disk-usage-in-python/16918>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 29, 2018, 12:33pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-out-free-disk-space-equivalent-to-psutil-disk-usage-in-python/16918 "2018-10-29T12:33:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ron\_Zaidman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ron_zaidman/32/5893_2.png) [@Ron\_Zaidman](https://discourse.julialang.org/u/Ron_Zaidman)\
**Post date:** [October 29, 2018, 12:33pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-out-free-disk-space-equivalent-to-psutil-disk-usage-in-python/16918/1 "2018-10-29T12:33:31Z")

</div>

Is there a way to find out free disk space in drive C: both on windows and linux ?  
On python I would that by calling  
psutil.disk\_usage(‘C:’).free

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [October 31, 2018, 8:50am UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-out-free-disk-space-equivalent-to-psutil-disk-usage-in-python/16918/2 "2018-10-31T08:50:57Z")

</div>

It seems not…

If you are OK with using python from Julia you could probably use:

```julia
julia> using PyCall

julia> @pyimport shutil

julia> shutil.disk_usage("/")

```

If you like to play with lower level (see [doc](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#)) you could write something similar to:

```julia
julia> function disk_free_space(path)
          if Sys.islinux() # maybe isunix would work here too?
            statfs = zeros(UInt64, 11) # warning: I am not sure all platform use unsigned long for all fields. See `man statvfs`
            ccall((:statvfs, "libm.so.6"), Cint, (Cstring,Ptr{Cvoid}), "/", statfs)
            convert(Int, statfs[1]*statfs[5])
          elseif Sys.iswindows()
             # you could call GetDiskFreeSpaceExA from kernel32.dll 
             # something like ->
             # ccall( (:GetDiskFreeSpaceExA, "kernel32.dll"), stdcall, ... )  
             # sry I have no windows to try
          else throw(ErrorException("Unsupported platform"))
          end
       end

```

---

<div class="post-metadata">

**Author:** ![Ron\_Zaidman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ron_zaidman/32/5893_2.png) [@Ron\_Zaidman](https://discourse.julialang.org/u/Ron_Zaidman)\
**Post date:** [October 31, 2018, 9:29am UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-out-free-disk-space-equivalent-to-psutil-disk-usage-in-python/16918/3 "2018-10-31T09:29:56Z")

</div>

Thanks I like the low level solution…

---

<div class="post-metadata">

**Author:** ![greghislop](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greghislop/32/30086_2.png) [@greghislop](https://discourse.julialang.org/u/greghislop)\
**Post date:** [January 30, 2020, 2:41am UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-out-free-disk-space-equivalent-to-psutil-disk-usage-in-python/16918/4 "2020-01-30T02:41:29Z")

</div>

So I made the silly mistake of blindly copying this code a few months back and incorporating it into a much larger code set. Sometime later I experienced a segfault that would occur during garbage collection on very rare occasions. This become really annoying when running many instances of said code and cost me a lot of time. The error ultimately was due to statvfs not returning only 11 values as UInt64 but some extra bytes as well. The moral of the story if you use this code pay attention to Liso’s warning:

warning: I am not sure all platform use unsigned long for all fields. See `man statvfs`

note also that man statvfs at least in my case on Ubuntu 18.04 gives obscure type names of fsblkcnt\_t and fsfilcnt\_t and even then says it’s only the approximate structure. Personally I don’t really need this call to be efficient so I’m using a `df .` call now.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [January 30, 2020, 3:21am UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-out-free-disk-space-equivalent-to-psutil-disk-usage-in-python/16918/5 "2020-01-30T03:21:04Z")

</div>

To expand on what Greg said…

The trick to calling this from julia is to find the real definition of statvfs in the system headers (the man page only says the struct is “approximately as follows …”). On my system, it’s in /usr/include/x86\_64-linux-gnu/bits/statvfs.h

But even then, there’s several `#ifdef`s which might or might not be turned on. Running the following C++ program gives you the true size of everything:

```nohighlight
#include <iostream>
#include <sys/statvfs.h>

int main()
{
    struct statvfs fs;
#define PRN_SZ(x) std::cout << "sizeof(" #x ") = " << sizeof(x) << "\n";
    PRN_SZ(fs);
    PRN_SZ(fs.f_bsize); /* Filesystem block size */
    PRN_SZ(fs.f_frsize); /* Fragment size */
    PRN_SZ(fs.f_blocks); /* Size of fs in f_frsize units */
    PRN_SZ(fs.f_bfree); /* Number of free blocks */
    PRN_SZ(fs.f_bavail); /* Number of free blocks for unprivileged users */
    PRN_SZ(fs.f_files); /* Number of inodes */
    PRN_SZ(fs.f_ffree); /* Number of free inodes */
    PRN_SZ(fs.f_favail); /* Number of free inodes for unprivileged users */
    PRN_SZ(fs.f_fsid); /* Filesystem ID */
    PRN_SZ(fs.f_flag); /* Mount flags */
    PRN_SZ(fs.f_namemax); /* Maximum filename length */
    PRN_SZ(fs.__f_spare);
    //if(statvfs("/", &fs) == 0) {
    // ...
    //}
    return 0;
}

```

On my system, the sizeof `statvfs` is 112 bytes which is way larger than 11x`UInt64`. So that can result in some fun memory corruption when the system tries to write into `fs.f_namemax` or something (there’s also private padding bytes in `__f_spare` which could be zeroed for all we know). Basically, the call to `statvfs` can trample over some random memory. In the crash from [Core dump inside LasIO · Issue #30 · visr/LasIO.jl · GitHub](https://github.com/visr/LasIO.jl/issues/30) that happened to be memory that the GC was using to track another object’s type. When that object eventually goes out of scope the GC tried to collect it and things blew up.

TBH I’m not sure what the “correct” and robust way to deal with system specific structs in headers like these are. The shape and size of `statvfs` can vary between different architectures.

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [January 30, 2020, 6:31am UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-out-free-disk-space-equivalent-to-psutil-disk-usage-in-python/16918/6 "2020-01-30T06:31:21Z")

</div>

If I remember well and if it is still true then Julia is using `libuv` library. It was problem that `libuv` missed this function, see for example this [issue](https://github.com/nodejs/node/issues/10745), but it seems to be resolved in this [commit](https://github.com/libuv/libuv/pull/2396/files) from august 2019.

It probably could be solved by `ccall` proper function from julia’s libuv. (optimally in julia standard ibrary).

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [January 30, 2020, 7:06am UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-out-free-disk-space-equivalent-to-psutil-disk-usage-in-python/16918/7 "2020-01-30T07:06:59Z")

</div>

Yes that’s a much better option. Looks like Julia’s libuv may not be carrying that patch yet though. See

- [unix,win: add uv\_fs\_statfs() by cjihrig · Pull Request #2396 · libuv/libuv · GitHub](https://github.com/libuv/libuv/pull/2396)
- [GitHub - JuliaLang/libuv: Cross-platform asynchronous I/O](https://github.com/julialang/libuv)

---

<div class="post-metadata">

**Author:** ![NHDaly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhdaly/32/1413_2.png) [@NHDaly](https://discourse.julialang.org/u/NHDaly)\
**Post date:** [August 13, 2020, 6:49pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-out-free-disk-space-equivalent-to-psutil-disk-usage-in-python/16918/8 "2020-08-13T18:49:44Z")

</div>

Filed an issue about this here:  
[https://github.com/JuliaLang/julia/issues/37038](https://github.com/JuliaLang/julia/issues/37038)
