Python shutil.disk_usage() Examples

The following are 30 code examples for showing how to use shutil.disk_usage(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module shutil , or try the search function .

Example 1
Project: speaksee   Author: aimagelab   File: field.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, preprocessing=None, postprocessing=None, detections_path=None, max_detections=100,
                 sort_by_prob=False, load_in_tmp=True):
        self.max_detections = max_detections
        self.detections_path = detections_path
        self.sort_by_prob = sort_by_prob

        tmp_detections_path = os.path.join('/tmp', os.path.basename(detections_path))

        if load_in_tmp:
            if not os.path.isfile(tmp_detections_path):
                if shutil.disk_usage("/tmp")[-1] < os.path.getsize(detections_path):
                    warnings.warn('Loading from %s, because /tmp has no enough space.' % detections_path)
                else:
                    warnings.warn("Copying detection file to /tmp")
                    shutil.copyfile(detections_path, tmp_detections_path)
                    warnings.warn("Done.")
                    self.detections_path = tmp_detections_path
            else:
                self.detections_path = tmp_detections_path

        super(ImageDetectionsField, self).__init__(preprocessing, postprocessing) 
Example 2
Project: meshed-memory-transformer   Author: aimagelab   File: field.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, preprocessing=None, postprocessing=None, detections_path=None, max_detections=100,
                 sort_by_prob=False, load_in_tmp=True):
        self.max_detections = max_detections
        self.detections_path = detections_path
        self.sort_by_prob = sort_by_prob

        tmp_detections_path = os.path.join('/tmp', os.path.basename(detections_path))

        if load_in_tmp:
            if not os.path.isfile(tmp_detections_path):
                if shutil.disk_usage("/tmp")[-1] < os.path.getsize(detections_path):
                    warnings.warn('Loading from %s, because /tmp has no enough space.' % detections_path)
                else:
                    warnings.warn("Copying detection file to /tmp")
                    shutil.copyfile(detections_path, tmp_detections_path)
                    warnings.warn("Done.")
                    self.detections_path = tmp_detections_path
            else:
                self.detections_path = tmp_detections_path

        super(ImageDetectionsField, self).__init__(preprocessing, postprocessing) 
Example 3
Project: psutil   Author: giampaolo   File: test_system.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_disk_usage(self):
        usage = psutil.disk_usage(os.getcwd())
        self.assertEqual(usage._fields, ('total', 'used', 'free', 'percent'))

        assert usage.total > 0, usage
        assert usage.used > 0, usage
        assert usage.free > 0, usage
        assert usage.total > usage.used, usage
        assert usage.total > usage.free, usage
        assert 0 <= usage.percent <= 100, usage.percent
        if hasattr(shutil, 'disk_usage'):
            # py >= 3.3, see: http://bugs.python.org/issue12442
            shutil_usage = shutil.disk_usage(os.getcwd())
            tolerance = 5 * 1024 * 1024  # 5MB
            self.assertEqual(usage.total, shutil_usage.total)
            self.assertAlmostEqual(usage.free, shutil_usage.free,
                                   delta=tolerance)
            self.assertAlmostEqual(usage.used, shutil_usage.used,
                                   delta=tolerance)

        # if path does not exist OSError ENOENT is expected across
        # all platforms
        fname = self.get_testfn()
        with self.assertRaises(FileNotFoundError):
            psutil.disk_usage(fname) 
Example 4
Project: cot   Author: glennmatthews   File: utilities.py    License: MIT License 6 votes vote down vote up
def available_bytes_at_path(path):
    """Get the available disk space in a given directory.

    Args:
      path (str): Directory path to check.

    Returns:
      int: Available space, in bytes

    Raises:
      OSError: if the specified path does not exist or is not readable.
    """
    if not os.path.exists(path):
        raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), path)
    if not os.path.isdir(path):
        raise OSError(errno.ENOTDIR, os.strerror(errno.ENOTDIR), path)
    available = disk_usage(path).free
    logger.debug("There appears to be %s available at %s",
                 pretty_bytes(available), path)
    return available 
Example 5
def _get_message_if_space_insufficient(cls, paths_to_copy):
        INSUFFIENT_SPACE_ERROR_FMT = "There is insufficient space remaining on this instance to " + \
        "containerize this notebook. Containerization would require {} of additional space."

        files_to_copy_bytes = cls._get_total_size_of_files(paths_to_copy)
        _, _, free_space_bytes = shutil.disk_usage("/")

        required_bytes = int(cls.REQUIRED_SPACE_PER_FILES_SPACE * files_to_copy_bytes
            ) + cls.SPACE_REQUIREMENT_FUDGE_BYTES

        if required_bytes > free_space_bytes:
            cls.logger.info("Insufficient space to containerize. Has {} bytes, requires {} bytes, " +
                "with fudge space requires {} bytes.".format(
                    free_space_bytes, files_to_copy_bytes, required_bytes))

            additional_required_bytes = required_bytes - free_space_bytes
            human_readable_additional_space_required = size(required_bytes - free_space_bytes)
            return INSUFFIENT_SPACE_ERROR_FMT.format("{} bytes ({})".format(
                additional_required_bytes, human_readable_additional_space_required)) 
Example 6
Project: dcos-e2e   Author: dcos   File: doctor.py    License: Apache License 2.0 6 votes vote down vote up
def _check_tmp_free_space() -> CheckLevels:
    """
    Warn if there is not enough free space in the default temporary directory.
    """
    free_space = shutil.disk_usage(gettempdir()).free
    free_space_gb = free_space / 1024 / 1024 / 1024

    low_space_message = (
        'The default temporary directory ("{tmp_prefix}") has '
        '{free_space:.1f} GB of free space available. '
        'Creating a cluster typically takes approximately 2 GB of temporary '
        'storage. '
        'If you encounter problems with disk space usage, set the ``TMPDIR`` '
        'environment variable to a suitable temporary directory or use the '
        '``--workspace-dir`` option on the ``minidcos docker create`` command.'
    ).format(
        tmp_prefix=Path('/') / gettempprefix(),
        free_space=free_space_gb,
    )

    if free_space_gb < 5:
        warn(message=low_space_message)
        return CheckLevels.WARNING

    return CheckLevels.NONE 
Example 7
Project: Carnets   Author: holzschu   File: data.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_free_space_in_dir(path):
    """
    Given a path to a directory, returns the amount of free space (in
    bytes) on that filesystem.

    Parameters
    ----------
    path : str
        The path to a directory

    Returns
    -------
    bytes : int
        The amount of free space on the partition that the directory
        is on.
    """
    if not os.path.isdir(path):
        raise OSError(
            "Can only determine free space associated with directories, "
            "not files.")
        # Actually you can on Linux but I want to avoid code that fails
        # on Windows only.
    return shutil.disk_usage(path).free 
Example 8
Project: multibootusb   Author: mbusb   File: usb.py    License: GNU General Public License v2.0 5 votes vote down vote up
def disk_usage(mount_path):
    """
    Return disk usage statistics about the given path as a (total, used, free)
    namedtuple.  Values are expressed in bytes.
    """
    # Author: Giampaolo Rodola' <g.rodola [AT] gmail [DOT] com>
    # License: MIT
    _ntuple_diskusage = collections.namedtuple('usage', 'total used free')

    if platform.system() == "Linux":
        st = os.statvfs(mount_path)
        free = st.f_bavail * st.f_frsize
        total = st.f_blocks * st.f_frsize
        used = (st.f_blocks - st.f_bfree) * st.f_frsize

        return _ntuple_diskusage(total, used, free)

    elif platform.system() == "Windows":

        _, total, free = ctypes.c_ulonglong(), ctypes.c_ulonglong(), \
                         ctypes.c_ulonglong()
        if sys.version_info >= (3,) or isinstance(mount_path, unicode):
            fun = ctypes.windll.kernel32.GetDiskFreeSpaceExW
        else:
            fun = ctypes.windll.kernel32.GetDiskFreeSpaceExA
        ret = fun(mount_path, ctypes.byref(_), ctypes.byref(total), ctypes.byref(free))
        if ret == 0:
            raise ctypes.WinError()
        used = total.value - free.value

        return _ntuple_diskusage(total.value, used, free.value)
    else:
        raise NotImplementedError("Platform not supported.") 
Example 9
Project: multibootusb   Author: mbusb   File: install.py    License: GNU General Public License v2.0 5 votes vote down vote up
def install_progress():
    """
    Function to calculate progress percentage of install.
    :return:
    """
    from . import progressbar
    try:
        usb_details = details(config.usb_disk)
    except PartitionNotMounted as e:
        log(str(e))
        return

    config.usb_mount = usb_details['mount_point']
    usb_size_used = usb_details['size_used']
    thrd = threading.Thread(target=install_distro, name="install_progress")
    # thrd.daemon()
    # install_size = usb_size_used / 1024
#     install_size = iso_size(config.image_path) / 1024
    final_size = (usb_size_used + iso_size(config.image_path)) + config.persistence
    thrd.start()
    pbar = progressbar.ProgressBar(maxval=100).start()  # bar = progressbar.ProgressBar(redirect_stdout=True)
    while thrd.is_alive():
        current_size = shutil.disk_usage(usb_details['mount_point'])[1]
        percentage = int((current_size / final_size) * 100)
        if percentage > 100:
            percentage = 100
        config.percentage = percentage
        pbar.update(percentage)
        time.sleep(0.1) 
Example 10
Project: gpt   Author: encarsia   File: modules.py    License: GNU General Public License v3.0 5 votes vote down vote up
def freespace(self, src, dest):
        """Check for free disc space"""
        import_size = 0
        for dirpath, dirnames, filenames in os.walk(src):
            for f in filenames:
                fp = os.path.join(dirpath, f)
                import_size += os.path.getsize(fp)
        if import_size < shutil.disk_usage(dest).free:
            return True
        else:
            self.needspace = app.sizeof_fmt(import_size - shutil.disk_usage(dest).free)
            return False

    # Zielordner wählen, neuen oder bestehenden Ordner, Defaultwert yyyy-mm-dd 
Example 11
Project: koku   Author: project-koku   File: aws_report_downloader.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def _check_size(self, s3key, check_inflate=False):
        """Check the size of an S3 file.

        Determine if there is enough local space to download and decompress the
        file.

        Args:
            s3key (str): the key name of the S3 object to check
            check_inflate (bool): if the file is compressed, evaluate the file's decompressed size.

        Returns:
            (bool): whether the file can be safely stored (and decompressed)

        """
        size_ok = False

        s3fileobj = self.s3_client.get_object(Bucket=self.report.get("S3Bucket"), Key=s3key)
        size = int(s3fileobj.get("ContentLength", -1))

        if size < 0:
            raise AWSReportDownloaderError(f"Invalid size for S3 object: {s3fileobj}")

        free_space = shutil.disk_usage(self.download_path)[2]
        if size < free_space:
            size_ok = True

        LOG.debug("%s is %s bytes; Download path has %s free", s3key, size, free_space)

        ext = os.path.splitext(s3key)[1]
        if ext == ".gz" and check_inflate and size_ok and size > 0:
            # isize block is the last 4 bytes of the file; see: RFC1952
            resp = self.s3_client.get_object(
                Bucket=self.report.get("S3Bucket"), Key=s3key, Range="bytes={}-{}".format(size - 4, size)
            )
            isize = struct.unpack("<I", resp["Body"].read(4))[0]
            if isize > free_space:
                size_ok = False

            LOG.debug("%s is %s bytes uncompressed; Download path has %s free", s3key, isize, free_space)

        return size_ok 
Example 12
Project: vnpy_crypto   Author: birforce   File: test_system.py    License: MIT License 5 votes vote down vote up
def test_disk_usage(self):
        usage = psutil.disk_usage(os.getcwd())
        self.assertEqual(usage._fields, ('total', 'used', 'free', 'percent'))

        assert usage.total > 0, usage
        assert usage.used > 0, usage
        assert usage.free > 0, usage
        assert usage.total > usage.used, usage
        assert usage.total > usage.free, usage
        assert 0 <= usage.percent <= 100, usage.percent
        if hasattr(shutil, 'disk_usage'):
            # py >= 3.3, see: http://bugs.python.org/issue12442
            shutil_usage = shutil.disk_usage(os.getcwd())
            tolerance = 5 * 1024 * 1024  # 5MB
            self.assertEqual(usage.total, shutil_usage.total)
            self.assertAlmostEqual(usage.free, shutil_usage.free,
                                   delta=tolerance)
            self.assertAlmostEqual(usage.used, shutil_usage.used,
                                   delta=tolerance)

        # if path does not exist OSError ENOENT is expected across
        # all platforms
        fname = tempfile.mktemp()
        with self.assertRaises(OSError) as exc:
            psutil.disk_usage(fname)
        self.assertEqual(exc.exception.errno, errno.ENOENT) 
Example 13
Project: vnpy_crypto   Author: birforce   File: test_system.py    License: MIT License 5 votes vote down vote up
def test_disk_usage_unicode(self):
        # See: https://github.com/giampaolo/psutil/issues/416
        if ASCII_FS:
            with self.assertRaises(UnicodeEncodeError):
                psutil.disk_usage(TESTFN_UNICODE) 
Example 14
Project: vnpy_crypto   Author: birforce   File: test_system.py    License: MIT License 5 votes vote down vote up
def test_disk_usage_bytes(self):
        psutil.disk_usage(b'.') 
Example 15
Project: telegram-upload   Author: Nekmo   File: utils.py    License: MIT License 5 votes vote down vote up
def free_disk_usage(directory='.'):
    return shutil.disk_usage(directory)[2] 
Example 16
Project: flyingcloud   Author: xBrite   File: file.py    License: Apache License 2.0 5 votes vote down vote up
def find_recursive_pattern(base_dir, pattern):
    for root, dirnames, filenames in os.walk(base_dir):
        for filename in fnmatch.filter(filenames, pattern):
            yield os.path.join(root, filename)


# disk_usage: Adapted from http://code.activestate.com/recipes/577972-disk-usage/ 
Example 17
Project: flyingcloud   Author: xBrite   File: file.py    License: Apache License 2.0 5 votes vote down vote up
def disk_usage(path):
        """Return disk usage statistics about the given path.

        Returned value is a named tuple with attributes 'total', 'used' and
        'free', which are the amount of total, used and free space, in bytes.
        """
        st = os.statvfs(path)
        free = st.f_bavail * st.f_frsize
        total = st.f_blocks * st.f_frsize
        used = (st.f_blocks - st.f_bfree) * st.f_frsize
        return _ntuple_diskusage(total, used, free) 
Example 18
Project: flyingcloud   Author: xBrite   File: file.py    License: Apache License 2.0 5 votes vote down vote up
def disk_usage(path):
        raise NotImplementedError("disk_usage not supported") 
Example 19
Project: psutil   Author: giampaolo   File: test_system.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_disk_usage_unicode(self):
        # See: https://github.com/giampaolo/psutil/issues/416
        with self.assertRaises(UnicodeEncodeError):
            psutil.disk_usage(UNICODE_SUFFIX) 
Example 20
Project: psutil   Author: giampaolo   File: test_system.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_disk_usage_bytes(self):
        psutil.disk_usage(b'.') 
Example 21
Project: funfuzz   Author: MozillaSecurity   File: bot.py    License: Mozilla Public License 2.0 5 votes vote down vote up
def print_machine_info():
    """Log information about the machine."""
    print(f'Platform details: {" ".join(platform.uname())}')

    hg_version = subprocess.run(["hg", "-q", "version"],
                                check=True,
                                stdout=subprocess.PIPE).stdout.decode("utf-8", errors="replace").rstrip()
    print(f"hg info: {hg_version}")

    if shutil.which("gdb"):
        gdb_version = subprocess.run(["gdb", "--version"],
                                     check=True,
                                     stdout=subprocess.PIPE).stdout.decode("utf-8", errors="replace").split("\n")[0]
        print(f"gdb info: {gdb_version}")
    if shutil.which("git"):
        git_version = subprocess.run(["git", "version"],
                                     check=True,
                                     stdout=subprocess.PIPE).stdout.decode("utf-8", errors="replace").rstrip()
        print(f"git info: {git_version}")
    print(f"Python version: {sys.version.split()[0]}")

    print(f"Number of cores visible to OS: {multiprocessing.cpu_count()}")
    rootdir_free_space = shutil.disk_usage("/").free / (1024 ** 3)
    print(f"Free space (GB): {rootdir_free_space:.2f}")

    hgrc_path = Path("~/.hg/hgrc").expanduser()
    if hgrc_path.is_file():
        print("The hgrc of this repository is:")
        with io.open(str(hgrc_path), "r", encoding="utf-8", errors="replace") as f:
            hgrc_contents = f.readlines()
        for line in hgrc_contents:
            print(line.rstrip())

    try:
        # resource library is only applicable to Linux or Mac platforms.
        import resource  # pylint: disable=import-error
        # pylint: disable=no-member
        print(f"Corefile size (soft limit, hard limit) is: {resource.getrlimit(resource.RLIMIT_CORE)!r}")
    except ImportError:
        print("Not checking corefile size as resource module is unavailable") 
Example 22
Project: teleport   Author: tp4a   File: audit.py    License: Apache License 2.0 5 votes vote down vote up
def get_free_space_bytes(folder):
    """ Return folder/drive free space (in bytes)
    """
    try:
        total, _, free = shutil.disk_usage(folder)
        return total, free
    except:
        return 0, 0 
Example 23
Project: dfirtriage   Author: travisfoley   File: DFIRtriage-v4-pub.py    License: The Unlicense 5 votes vote down vote up
def freespace_check(drive):
    """Check for free space on given drive"""
    usage = shutil.disk_usage(drive)
    if (usage.free // (2**30)) < MINDISKSPACE:
        if ARGS.nomem:
            print("\n\t[-] Free space is {} GB\n".format(usage.free // (2**30)), flush=True)
            print("\n[!] Disk space low on target, good thing you weren't wanting memory", flush=True)
        else:
            print("\n\t[-] Free space is {} GB\n".format(usage.free // (2**30)), flush=True)
            print("\n[!] Disk space low on target, memory will need to be skipped", flush=True)
            ARGS.nomem = True 
Example 24
Project: palanaeum   Author: Palanaeum   File: admin_views.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def index(request):
    usage = shutil.disk_usage(settings.MEDIA_ROOT)
    total_space_str = "{:.2f} Gb".format(usage.total / 1024**3)
    used_space_str = "{:.2f} Gb".format(usage.used / 1024**3)
    free_space_str = "{:.2f} Gb".format(usage.free / 1024**3)
    return render(request, 'palanaeum/admin/admin_index.html',
                  {'used_space': usage.used, 'available_space': usage.free,
                   'total_space': usage.total, 'total_space_str': total_space_str,
                   'free_space_str': free_space_str, 'used_space_str': used_space_str}) 
Example 25
Project: Fluid-Designer   Author: Microvellum   File: test_shutil.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_disk_usage(self):
        usage = shutil.disk_usage(os.getcwd())
        self.assertGreater(usage.total, 0)
        self.assertGreater(usage.used, 0)
        self.assertGreaterEqual(usage.free, 0)
        self.assertGreaterEqual(usage.total, usage.used)
        self.assertGreater(usage.total, usage.free) 
Example 26
Project: Fluid-Designer   Author: Microvellum   File: test_shutil.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_module_all_attribute(self):
        self.assertTrue(hasattr(shutil, '__all__'))
        target_api = ['copyfileobj', 'copyfile', 'copymode', 'copystat',
                      'copy', 'copy2', 'copytree', 'move', 'rmtree', 'Error',
                      'SpecialFileError', 'ExecError', 'make_archive',
                      'get_archive_formats', 'register_archive_format',
                      'unregister_archive_format', 'get_unpack_formats',
                      'register_unpack_format', 'unregister_unpack_format',
                      'unpack_archive', 'ignore_patterns', 'chown', 'which',
                      'get_terminal_size', 'SameFileError']
        if hasattr(os, 'statvfs') or os.name == 'nt':
            target_api.append('disk_usage')
        self.assertEqual(set(shutil.__all__), set(target_api)) 
Example 27
Project: ironpython3   Author: IronLanguages   File: test_shutil.py    License: Apache License 2.0 5 votes vote down vote up
def test_disk_usage(self):
        usage = shutil.disk_usage(os.getcwd())
        self.assertGreater(usage.total, 0)
        self.assertGreater(usage.used, 0)
        self.assertGreaterEqual(usage.free, 0)
        self.assertGreaterEqual(usage.total, usage.used)
        self.assertGreater(usage.total, usage.free) 
Example 28
Project: ironpython3   Author: IronLanguages   File: test_shutil.py    License: Apache License 2.0 5 votes vote down vote up
def test_module_all_attribute(self):
        self.assertTrue(hasattr(shutil, '__all__'))
        target_api = ['copyfileobj', 'copyfile', 'copymode', 'copystat',
                      'copy', 'copy2', 'copytree', 'move', 'rmtree', 'Error',
                      'SpecialFileError', 'ExecError', 'make_archive',
                      'get_archive_formats', 'register_archive_format',
                      'unregister_archive_format', 'get_unpack_formats',
                      'register_unpack_format', 'unregister_unpack_format',
                      'unpack_archive', 'ignore_patterns', 'chown', 'which',
                      'get_terminal_size', 'SameFileError']
        if hasattr(os, 'statvfs') or os.name == 'nt':
            target_api.append('disk_usage')
        self.assertEqual(set(shutil.__all__), set(target_api)) 
Example 29
Project: docker-box   Author: MicroPyramid   File: utils.py    License: MIT License 5 votes vote down vote up
def disk_size(self):
        x = shutil.disk_usage('/')
        return format(x.total / 1024 / 1024 / 1024, '.2f') 
Example 30
Project: docker-box   Author: MicroPyramid   File: utils.py    License: MIT License 5 votes vote down vote up
def free_space(self):
        x = shutil.disk_usage('/')
        return format(x.free / 1024 / 1024 / 1024, '.2f')