當前位置: 首頁>>代碼示例>>Python>>正文


Python shutil.disk_usage方法代碼示例

本文整理匯總了Python中shutil.disk_usage方法的典型用法代碼示例。如果您正苦於以下問題:Python shutil.disk_usage方法的具體用法?Python shutil.disk_usage怎麽用?Python shutil.disk_usage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在shutil的用法示例。


在下文中一共展示了shutil.disk_usage方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
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) 
開發者ID:aimagelab,項目名稱:speaksee,代碼行數:23,代碼來源:field.py

示例2: test_disk_usage

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
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) 
開發者ID:giampaolo,項目名稱:psutil,代碼行數:27,代碼來源:test_system.py

示例3: available_bytes_at_path

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
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 
開發者ID:glennmatthews,項目名稱:cot,代碼行數:22,代碼來源:utilities.py

示例4: _get_message_if_space_insufficient

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
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)) 
開發者ID:awslabs,項目名稱:aws-iot-analytics-notebook-containers,代碼行數:21,代碼來源:kernel_image_creator.py

示例5: _check_tmp_free_space

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
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 
開發者ID:dcos,項目名稱:dcos-e2e,代碼行數:27,代碼來源:doctor.py

示例6: get_free_space_in_dir

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
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 
開發者ID:holzschu,項目名稱:Carnets,代碼行數:25,代碼來源:data.py

示例7: disk_usage

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
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.") 
開發者ID:mbusb,項目名稱:multibootusb,代碼行數:35,代碼來源:usb.py

示例8: install_progress

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
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) 
開發者ID:mbusb,項目名稱:multibootusb,代碼行數:31,代碼來源:install.py

示例9: freespace

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
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 
開發者ID:encarsia,項目名稱:gpt,代碼行數:16,代碼來源:modules.py

示例10: _check_size

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
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 
開發者ID:project-koku,項目名稱:koku,代碼行數:43,代碼來源:aws_report_downloader.py

示例11: test_disk_usage

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
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) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:28,代碼來源:test_system.py

示例12: test_disk_usage_unicode

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
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) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:7,代碼來源:test_system.py

示例13: test_disk_usage_bytes

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
def test_disk_usage_bytes(self):
        psutil.disk_usage(b'.') 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:4,代碼來源:test_system.py

示例14: free_disk_usage

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
def free_disk_usage(directory='.'):
    return shutil.disk_usage(directory)[2] 
開發者ID:Nekmo,項目名稱:telegram-upload,代碼行數:4,代碼來源:utils.py

示例15: find_recursive_pattern

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import disk_usage [as 別名]
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/ 
開發者ID:xBrite,項目名稱:flyingcloud,代碼行數:9,代碼來源:file.py


注:本文中的shutil.disk_usage方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。