获取文件大小

os.path.getsize(file_path):file_path为文件路径

  1. >>> import os
  2. >>> os.path.getsize('d:/svn/bin/SciLexer.dll')
  3. 1479904

获取文件夹大小

遍历文件夹,将所有文件大小加和。遍历文件夹使用os.walk函数

  1. import os
  2. from os.path import join, getsize
  3.  
  4. def getdirsize(dir):
  5. size = 0
  6. for root, dirs, files in os.walk(dir):
  7. size += sum([getsize(join(root, name)) for name in files])
  8. return size
  9.  
  10. if __name__ == '__main__':
  11. size = getdirsize(r'D:\svn')
  12. print('There are %.3f' % (size / 1024 / 1024), 'Mbytes in D:\\svn')\
  13. 执行结果:
  1.  

help(os.walk)获取帮助信息

  1. Help on function walk in module os:
  2.  
  3. walk(top, topdown=True, onerror=None, followlinks=False)
  4. Directory tree generator.
  5.  
  6. For each directory in the directory tree rooted at top (including top
  7. itself, but excluding '.' and '..'), yields a 3-tuple
  8.  
  9. dirpath, dirnames, filenames
  10.  
  11. dirpath is a string, the path to the directory. dirnames is a list of
  12. the names of the subdirectories in dirpath (excluding '.' and '..').
  13. filenames is a list of the names of the non-directory files in dirpath.
  14. Note that the names in the lists are just names, with no path components.
  15. To get a full path (which begins with top) to a file or directory in
  16. dirpath, do os.path.join(dirpath, name).
  17.  
  18. If optional arg 'topdown' is true or not specified, the triple for a
  19. directory is generated before the triples for any of its subdirectories
  20. (directories are generated top down). If topdown is false, the triple
  21. for a directory is generated after the triples for all of its
  22. subdirectories (directories are generated bottom up).
  23.  
  24. When topdown is true, the caller can modify the dirnames list in-place
  25. (e.g., via del or slice assignment), and walk will only recurse into the
  26. subdirectories whose names remain in dirnames; this can be used to prune the
  27. search, or to impose a specific order of visiting. Modifying dirnames when
  28. topdown is false is ineffective, since the directories in dirnames have
  29. already been generated by the time dirnames itself is generated. No matter
  30. the value of topdown, the list of subdirectories is retrieved before the
  31. tuples for the directory and its subdirectories are generated.
  32.  
  33. By default errors from the os.scandir() call are ignored. If
  34. optional arg 'onerror' is specified, it should be a function; it
  35. will be called with one argument, an OSError instance. It can
  36. report the error to continue with the walk, or raise the exception
  37. to abort the walk. Note that the filename is available as the
  38. filename attribute of the exception object.
  39.  
  40. By default, os.walk does not follow symbolic links to subdirectories on
  41. systems that support them. In order to get this functionality, set the
  42. optional argument 'followlinks' to true.
  43.  
  44. Caution: if you pass a relative pathname for top, don't change the
  45. current working directory between resumptions of walk. walk never
  46. changes the current directory, and assumes that the client doesn't
  47. either.
  48.  
  49. Example:
  50.  
  51. import os
  52. from os.path import join, getsize
  53. for root, dirs, files in os.walk('python/Lib/email'):
  54. print(root, "consumes", end="")
  55. print(sum([getsize(join(root, name)) for name in files]), end="")
  56. print("bytes in", len(files), "non-directory files")
  57. if 'CVS' in dirs:
  58. dirs.remove('CVS') # don't visit CVS directories

***********************************************************

学习永远不晚。——高尔基

***********************************************************

python3获取文件及文件夹大小的更多相关文章

  1. python获取文件及文件夹大小

    Python3.3下测试通过 获取文件大小 使用os.path.getsize函数,参数是文件的路径 获取文件夹大小 import os from os.path import join, getsi ...

  2. C#获取文件和文件夹大小

    代码如下: /// <summary> /// 获取文件夹大小 /// </summary> /// <param name="dirPath"> ...

  3. python 获取文件和文件夹大小

    1.os.path.getsize可以获取文件大小 >>> import os >>> file_name = 'E:\chengd\Cd.db' >> ...

  4. Python---进阶---文件操作---获取文件夹下所有文件的数量和大小

    一.####编写一个程序,统计当前目录下每个文件类型的文件数 ####思路: - 打开当前的文件夹 - 获取到当前文件夹下面所有的文件 - 处理我们当前的文件夹下面可能有文件夹的情况(也打印出来) - ...

  5. Linux C++获取文件夹大小

    项目中要计算指定文件夹的大小.百度查到这篇文章,https://my.oschina.net/Tsybius2014/blog/330628方法可行,运行正确. 拿到我们的项目中,却遇到一些问题:程序 ...

  6. iOS获取文件和文件夹大小

    //通常用于删除缓存的时,计算缓存大小 //单个文件的大小 - (long long) fileSizeAtPath:(NSString*) filePath{ NSFileManager* mana ...

  7. Windows Store App JavaScript 开发:获取文件和文件夹列表

    在应用程序中有时可能需要获取用户库中的内容,以便执行相关的操作.如果要获取某个用户库中的内容,需要先获取到这个用户库,获得用户库可以通过Windows.Storage命名空间中的KnownFolder ...

  8. python计算文件夹大小(linux du命令 简化版)

    C盘又满了,怎么办?用了一些垃圾清理软件(或者bat脚本),但是还是不理想,那么具体哪些文件夹下面有巨大的文件呢?windows并不能通过详细信息看到每个文件夹的大小(PS:这里所谓的文件夹的大小是指 ...

  9. Win10系列:JavaScript获取文件和文件夹列表

    在应用程序中有时可能需要获取用户库中的内容,以便执行相关的操作.如果要获取某个用户库中的内容,需要先获取到这个用户库,获得用户库可以通过Windows.Storage命名空间中的KnownFolder ...

随机推荐

  1. JAVAEE——SSH项目实战01:SVN介绍、eclipse插件安装和使用方法

    1 学习目标 1.掌握svn服务端.svn客户端.svn eclipse插件安装方法 2.掌握svn的基本使用方法 2 svn介绍 2.1 项目管理中的版本控制问题 通常软件开发由多人协作开发,如果对 ...

  2. oracle--ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源, 或者超时失效

    SELECT sid, serial#, username, osuser FROM v$session where sid in(select session_id from v$locked_ob ...

  3. 树莓派Pi2 使用入门

    1. 材料和环境 树莓派Pi2, microSD卡(大于等于4G), 网线 官网下载: 系统镜像 Raspbian Jessie (https://downloads.raspberrypi.org/ ...

  4. mysql的又一个让人捉摸不透的bug?

    这次就不说很多没有写博客了,因为前几天已经写过了.\^o^/ 昨天我们刚讨论了关于自动化运维工作的实现方式,如果批量执行,中间出错怎么办?突然有人提出mysql支持--force,可以跳过出错继续执行 ...

  5. HTTP 错误 401.3 - Unauthorized asp.net mvc 图片,css,js没有权限访问

    一.在服务器上发布了一个asp.net的网站,结果是页面可以显示,但是css,js,images无法访问,报错是没有权限,HTTP 错误 401.3 - Unauthorized 二.根据以往的经验, ...

  6. JSP中文乱码问题终极解决方案

    在介绍方法之前我们首先应该清楚具体的问题有哪些,笔者在本博客当中论述的JSP中文乱码问题有如下几个方面:页面乱码.参数乱码.表单乱码.源文件乱码.下面来逐一解决其中的乱码问题. 一.JSP页面中文乱码 ...

  7. JList动态添加元素

    JList动态添加元素   http://www.cnblogs.com/tianguook/archive/2012/01/31/2333992.html https://zhuanlan.zhih ...

  8. 邮件客户端修改密码—OWA

    邮件客户端修改密码—OWA 1.登录OWA 2.输入用户名 3.点击选项 4.更改密码

  9. python openpyxl.md

    Openpyxl 创建一个工作簿 下面是创建一个工作簿.而每个工作簿至少一个工作表我们可以通过active获取正在运行的工作表. In [1]: from openpyxl import Workbo ...

  10. oracle11g dataguard 备库数据同步的检查方法

    概述: 一.环境      主库:       ip地址:192.168.122.203       oracle根目录:/data/db/oracle       SID:qyq       数据文 ...