python3获取文件及文件夹大小
获取文件大小
os.path.getsize(file_path):file_path为文件路径
>>> import os
>>> os.path.getsize('d:/svn/bin/SciLexer.dll')
1479904
获取文件夹大小
遍历文件夹,将所有文件大小加和。遍历文件夹使用os.walk函数
import os
from os.path import join, getsize def getdirsize(dir):
size = 0
for root, dirs, files in os.walk(dir):
size += sum([getsize(join(root, name)) for name in files])
return size if __name__ == '__main__':
size = getdirsize(r'D:\svn')
print('There are %.3f' % (size / 1024 / 1024), 'Mbytes in D:\\svn')\
执行结果:
help(os.walk)获取帮助信息
Help on function walk in module os: walk(top, topdown=True, onerror=None, followlinks=False)
Directory tree generator. For each directory in the directory tree rooted at top (including top
itself, but excluding '.' and '..'), yields a 3-tuple dirpath, dirnames, filenames dirpath is a string, the path to the directory. dirnames is a list of
the names of the subdirectories in dirpath (excluding '.' and '..').
filenames is a list of the names of the non-directory files in dirpath.
Note that the names in the lists are just names, with no path components.
To get a full path (which begins with top) to a file or directory in
dirpath, do os.path.join(dirpath, name). If optional arg 'topdown' is true or not specified, the triple for a
directory is generated before the triples for any of its subdirectories
(directories are generated top down). If topdown is false, the triple
for a directory is generated after the triples for all of its
subdirectories (directories are generated bottom up). When topdown is true, the caller can modify the dirnames list in-place
(e.g., via del or slice assignment), and walk will only recurse into the
subdirectories whose names remain in dirnames; this can be used to prune the
search, or to impose a specific order of visiting. Modifying dirnames when
topdown is false is ineffective, since the directories in dirnames have
already been generated by the time dirnames itself is generated. No matter
the value of topdown, the list of subdirectories is retrieved before the
tuples for the directory and its subdirectories are generated. By default errors from the os.scandir() call are ignored. If
optional arg 'onerror' is specified, it should be a function; it
will be called with one argument, an OSError instance. It can
report the error to continue with the walk, or raise the exception
to abort the walk. Note that the filename is available as the
filename attribute of the exception object. By default, os.walk does not follow symbolic links to subdirectories on
systems that support them. In order to get this functionality, set the
optional argument 'followlinks' to true. Caution: if you pass a relative pathname for top, don't change the
current working directory between resumptions of walk. walk never
changes the current directory, and assumes that the client doesn't
either. Example: import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
print(root, "consumes", end="")
print(sum([getsize(join(root, name)) for name in files]), end="")
print("bytes in", len(files), "non-directory files")
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
***********************************************************
学习永远不晚。——高尔基
***********************************************************
python3获取文件及文件夹大小的更多相关文章
- python获取文件及文件夹大小
Python3.3下测试通过 获取文件大小 使用os.path.getsize函数,参数是文件的路径 获取文件夹大小 import os from os.path import join, getsi ...
- C#获取文件和文件夹大小
代码如下: /// <summary> /// 获取文件夹大小 /// </summary> /// <param name="dirPath"> ...
- python 获取文件和文件夹大小
1.os.path.getsize可以获取文件大小 >>> import os >>> file_name = 'E:\chengd\Cd.db' >> ...
- Python---进阶---文件操作---获取文件夹下所有文件的数量和大小
一.####编写一个程序,统计当前目录下每个文件类型的文件数 ####思路: - 打开当前的文件夹 - 获取到当前文件夹下面所有的文件 - 处理我们当前的文件夹下面可能有文件夹的情况(也打印出来) - ...
- Linux C++获取文件夹大小
项目中要计算指定文件夹的大小.百度查到这篇文章,https://my.oschina.net/Tsybius2014/blog/330628方法可行,运行正确. 拿到我们的项目中,却遇到一些问题:程序 ...
- iOS获取文件和文件夹大小
//通常用于删除缓存的时,计算缓存大小 //单个文件的大小 - (long long) fileSizeAtPath:(NSString*) filePath{ NSFileManager* mana ...
- Windows Store App JavaScript 开发:获取文件和文件夹列表
在应用程序中有时可能需要获取用户库中的内容,以便执行相关的操作.如果要获取某个用户库中的内容,需要先获取到这个用户库,获得用户库可以通过Windows.Storage命名空间中的KnownFolder ...
- python计算文件夹大小(linux du命令 简化版)
C盘又满了,怎么办?用了一些垃圾清理软件(或者bat脚本),但是还是不理想,那么具体哪些文件夹下面有巨大的文件呢?windows并不能通过详细信息看到每个文件夹的大小(PS:这里所谓的文件夹的大小是指 ...
- Win10系列:JavaScript获取文件和文件夹列表
在应用程序中有时可能需要获取用户库中的内容,以便执行相关的操作.如果要获取某个用户库中的内容,需要先获取到这个用户库,获得用户库可以通过Windows.Storage命名空间中的KnownFolder ...
随机推荐
- UI第二组与数据库对接时遇到的问题记录
此为组内某一位做UI的同学的随笔. 之前的app由于没有加入数据库,所以每次重新启动里面的东西都会回到初始状态,即不能保存内容.我们的数据库小组已经很棒地基本完成了数据库的工作,于是我就准备加入数据库 ...
- linux安装activemq
ActiveMQ是由Apache出品的,一款最流行的,能力强劲的开源消息总线.ActiveMQ是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,它非常快速,支持多种语言 ...
- Python邮件发送源码
-- coding:utf-8 -- i = 0 while i < 10: #发送十次 import smtplib from email.mime.text import MIMEText ...
- KB和KiB的区别
差别是KB等单位以10为底数的指数,KiB是以2为底数的指数. K 与 Ki 分别表示 kilo-(千) 与 kibi-(二进制千) .作为前缀使用时, k 表示 1,000,Ki 表示1,024. ...
- October 17th 2017 Week 42nd Tuesday
We execuse our sloth under the pretext of difficulty. 我们常以困难为由,作为懒惰的借口. The process of my system-tra ...
- 处理AsyncTask的内存泄漏问题
强引用AsyncTask导致了内存泄漏如下图 1.原因:activity销毁之后,AsyncTask线程可能依旧在执行,导致内存泄漏. 2.解决方法:查了一下大概有两个,一个是将函数声明为static ...
- 前端工程构建工具之Yeoman
一.Yeoman 简介 通常在开发新项目时我们都需要配置工程环境,开发目录,需要下载一些库.框架文件(如 jQuery.Backbone 等),配置编译环境(Less.Sass.Coffeescrip ...
- Tengine 2.1.2 (nginx/1.6.2)安装配置,淘宝 Web 服务器
简介 Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很 ...
- InnerClass annotations are missing corresponding EnclosingMember annotations. Such InnerClas...
如果 你的项目中使用了注解插件 比如butterknife 升级3.1之后打包编译 出现以下错误提示 InnerClass annotations are missing correspondi ...
- 【转】通过blob获取图像并显示
HTML代码: <div id="forAppend" class="demo"></div> JS代码: var eleAppend ...