Python计算文件大小

2023-05-16

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
#------------------------------------------------------------------------------
# file: 统计文件大小.py
# Date: 2019/11/19
#------------------------------------------------------------------------------
"""




import os


def formatsize(bytes):
    try:
        bytes = float(bytes)  # 默认字节
        kb = bytes / 1024  # 换算KB
    except:
        print("字节格式有误")
        return "Error"


    if kb >= 1024:
        M = kb / 1024  # KB换成M
        if M >= 1024:
            G = M / 1024
            return "%fG" % G
        else:
            return "%fM" % M
    else:
        return "%fkb" % kb


# 获取文件大小
def Getfile(path):
    try:
        size = os.path.getsize(path)
        return formatsize(size)
    except:
        print("获取文件大小错误")


# 获取目录总大小
def Getdir(filepath):  # 定义函数
    sum = 0  # 初始化文件大小
    try:
        filename = os.walk(filepath)  # 获取文件夹目录
        for root, dirs, files in filename:  # 循环遍历文件夹目录下的文件
            for fle in files:
                filesdirs = os.path.join(root, fle)  # 必须要这一步,不然获取的文件没有找到路径.
                filesize = os.path.getsize(filesdirs)  # 统计循环出来的文件大小
                sum += filesize   # 所有文件加起来总和
        return formatsize(sum)  # 调用上面formatsize的算法.
    except:
        print("获取文件夹大小错误")


if __name__ == "__main__":
    print("文件大小:" + Getfile("D:\电影\[电影天堂-www.dy2018.net].夺宝联盟.720p.BD中文字幕.rmvb"))
    print("目录大小: " + Getdir("D:\电影"))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python计算文件大小 的相关文章

随机推荐

  • 通俗说Openvswitch

    Openvswitch xff0c 顾名思义 xff0c Open xff0c 开源的 xff0c v xff0c virtual xff0c 虚拟的 xff0c switch交换机 通俗的讲就是一款开源的软件 xff0c 可以创建虚拟的交
  • 人间还是仙界?聊一聊linux系统的用户空间和内核空间

    我们生活在人间 xff0c 但 西游记 里提到 xff0c 在天上还有一个仙界 人间不知道仙界的存在 xff1b 而仙界知道人间的存在 xff0c 神仙也可以从仙界下凡到人间 xff0c 但是被严格管控的 软件设计的灵感其实都来自于生活 x
  • 什么是实时数据库?

    实时数据库是数据库系统发展的一个分支 xff0c 它适用于处理不断更新的快速变化的数据及具有时间 限制的事务处理 实时数据库技术是实时系统和数据库技术相结合的产物 xff0c 研究人员希望利用数据库 技术来解决实时系统中的数据管理问题 xf
  • 带你阅读linux内核源码:linux内核源代码编程规范

    linux内核代码是许许多多遵循相同内核开发规范的牛人们的共同的创造的结晶 作为一名linux内核或者驱动开发工程师 xff0c 很有必要了解这些内核开发规范 好处有以下几个 xff1a 这些约定或者规范对我们阅读linux内核源码 了解设
  • linux进程上下文、中断上下文介绍,以及为什么软中断不能睡眠?

    linux内核的软中断处理程序中能不能睡眠 xff1f 这是一个值得讨论的问题 答案其实很简单 xff0c 那就是不能 因为Linux的软中断处理程序的运行上下文有可能是中断上下文 xff08 注意此处是有可能 xff0c 而并非一定 xf
  • VS2008用devenv.com命令行工具自动编译工程

    转自 xff1a http www cr173 com html 18500 1 html 在vs2008下面提供了devenv com命令行方式 我们可以从VS安装目录 MicrosoftVisual Studio 9 Common7 I
  • 使用ICMP协议检测网络状态

    ICMP xff08 Internet ControlMessages Protocol xff0c 网间控制报文协议 xff09 是TCP IP协议族的子协议 xff0c 是一种面向无连接的协议 xff0c 在IP和路由器之前传递控制消息
  • c++打印enum class

    span class token keyword enum span span class token keyword class span span class token class name A span span class tok
  • 使用strace查找Emacs启动阻塞的原因(exec-path-from-shell)

    原文地址 https www lujun9972 win blog 2019 09 26 使用strace查找emacs启动阻塞的原因 exec path from shell index html 之前就觉得我的Emacs启动好慢 xff
  • 为Linux安装虚拟PDF打印机

    原文地址 https lujun9972 github io blog 2020 04 11 为linux安装虚拟pdf打印机 index html 今天发现一个 CUPS PDF 项目 可以为 CUPS Common Unix Print
  • ubuntu系统启用shell远程登陆

    Ubuntu desktop系统安装后 xff0c 想使用shell远程登陆 xff0c 会提示 Connecting to 192 168 220 133 22 Could not connect to 39 192 168 220 13
  • 枚举类(ENUM)用法总结

    对于ENUM一直是比较陌生的 xff0c 在和某酷爱ENUM的大神合作时 xff0c 才慢慢接触到ENUM的用法 1 ENUM是什么 xff1f 首先ENUM是一个类 xff0c 不像String int之类的数据结构 xff0c 更类似于
  • Python循环结构练习2

    Problem A xff1a 循环结构 输出数列2 xff0c 5 xff0c 8 xff0c 11 xff0c 14 题目描述 输入正整数n xff08 n 100 xff09 xff0c 输出数列2 xff0c 5 xff0c 8 x
  • KVM网络模型之:PCI Passthrough

    目录 PCI Passthrough技术介绍和KVM中配置 案例 内核启用 重新启动虚拟机实例 PCI Passthrough技术介绍和KVM中配置 PCI Passthrough技术是虚拟化网卡的终极解决方案 xff0c 能够让虚拟机独占
  • 微信开放公众平台,扩展自定义类,定时提醒,定时发消息

    微信开放公众平台 xff0c 扩展自定义类 xff0c 定时提醒 xff0c 定时发消息 lt php class MyapiAction extends BaseAction public function index 微医疗 预约提醒
  • Ubuntu配置iptables规则

    Ubuntu配置防火墙 xff0c 并且开机iptables自启动规则 适用于CentOS 1 登录root账号 span class token comment 切换到root账号 span super 64 super span cla
  • Linux记录用户执行命令

    span class token shebang important bin bash span span class token comment By lumia98 64 vip qq com span span class token
  • Nginx规则配置实例

    配置某个ip或者页面禁止访问及跳转方法 server listen 80 server name www test com cn location proxy redirect off proxy set header host host
  • Debian 11.2安装ssh服务

    切换到root用户 更新软件源 span class token function apt get span update 安装ssh span class token function apt get span span class to
  • Python计算文件大小

    span class token comment usr bin env python span span class token comment coding utf 8 span span class token triple quot