获取文件大小

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获取文件及文件夹大小的更多相关文章

  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. PostMan请求不到接口问题

    在些接口的时候经常需要调试,调试的有很多选择,比如swagger.postman,我就是使用过两个都用:为了避免被swagger坑到就再去用postman试试确认看行不行,结果太小白了还是遇到了一些问 ...

  2. 【SPL标准库专题(9)】 Datastructures:SplObjectStorage

    PHP SPL SplObjectStorage是用来存储一组对象的,特别是当你需要唯一标识对象的时候. PHP SPL SplObjectStorage类实现了Countable,Iterator, ...

  3. od 转储 二进制文件常用命令

    od :  NAME od - dump files in octal and other formats 常用命令: ➜ Downloads od -t x1 -Ax /etc/ld.so.cach ...

  4. Redis学习---面试基础知识点总结

    [学习参考] https://www.toutiao.com/i6566017785078481422/ https://www.toutiao.com/i6563232898831352323/ 0 ...

  5. 开源作业调度框架 - Quartz.NET - Cron表达式测试

    昨天简单写了一下如何使用Quzrtz.NET. 那么问题来了,我设置了Cron表达式之后如何知道是表达式是否按照预期的时间执行了呢? 我找到了些Cron表达式工具生成了表达式,确发现它们基本上没有进行 ...

  6. 2018.08.31 19:41 自学go语言

    有的人是从最基础的开始学,而我却是从最简单开始学,学着调试,学着编程,其实我也是编程小白,好多的不懂,我不明白很多都可以用云完成了,为什么还要继续自己编程,不明白,但是有需求吧,有需求是件好事情,说明 ...

  7. 关于MVC开发时,无法找到area的问题记录

    解决方法: 检查area=admin 的dll是否生成,一般都是admin域生成dll导致

  8. div+css ie6图片之间有间隙的问题

    图片转换为快级元素就解决了 img{display:block;} 也可设置img属性img{vertical-align:top;}

  9. 线程同步方式之互斥量Mutex

    互斥量和临界区非常相似,只有拥有了互斥对象的线程才可以访问共享资源,而互斥对象只有一个,因此可以保证同一时刻有且仅有一个线程可以访问共享资源,达到线程同步的目的. 互斥量相对于临界区更为高级,可以对互 ...

  10. python string.md

    string 包含用于处理文本的常量和类.string模块始于Python的最早版本. 2.0版本中, 许多之前只在模块中实现的函数被转移为string对象的方法. 之后的版本中, 虽然这些函数仍然可 ...