Check free disk space from Matlab/Octave

This Matlab/Octave function checks free disk space. For a long-running program, helps avoid totally filling a hard drive.

function free=diskfree(mydir)
% returns disk free space in bytes
% example:  diskfree('~')
try
  free = py.shutil.disk_usage(py.os.path.expanduser(mydir)).free;
catch
  mydir = ["'",mydir,"'"];
  cmd = ['python -c "import os,shutil; print(shutil.disk_usage(os.path.expanduser(',mydir,')).free)"'];
  [err, free] = system(cmd);
end

free = str2double(free);

end %function

Leave a Comment