Before I re-invent this particular wheel, has anybody got a nice routine for calculating the size of a directory using Python? It would be very nice if the routine would format the size nicely in Mb/Gb etc.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
If you are in Windows OS you can do:
install the module pywin32 by launching:
pip install pywin32
and then coding the following:
monknut answer is good but it fails on broken symlink, so you also have to check if this path really exists
For the second part of the question
Python 3.5 recursive folder size using
os.scandir
Chris' answer is good but could be made more idiomatic by using a set to check for seen directories, which also avoids using an exception for control flow:
use library sh: the module
du
does it:if you want to pass asterix, use
glob
as described here.to convert the values in human readables, use humanize: