Nov-29-2020, 08:57 AM
Modern Python:
So if you don't optimize to list 10_000_000 files,
You should read this: https://realpython.com/python-pathlib/
The
from pathlib import Path data_path = Path('c:\\02') for path in data_path.rglob("*"): if path.is_file(): ... if path.is_dir(): parent = path.parent name = path.name size_mib = path.stat().st_size / 1024**2 if size_mib < 1: # skipping files with less than 1 MiB continue print(name, "->", size_mib)The first party module
pathlib
has a better abstraction for paths.So if you don't optimize to list 10_000_000 files,
pathlib
would do its job.You should read this: https://realpython.com/python-pathlib/
The
rglob
is a recursive glob
. It returns a generator and the generator yields Path objects. You can work with Path objects. For example you can get the parent (which is a dir, if the path was pointing to a file). You can get also the stat from files.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!