简体   繁体   中英

How to find the size of all files in a directory and all its sub-directories?

I'm trying to print the name and the size of all files in a directory and all its sub-directories, but it only prints the name and size of the files in the first directory but not the sub-directories. Any help will be appreciated.

import os
path = os.getcwd()
walk_method = os.walk(path)
while True:
    try:
        p, sub_dir, files = next(walk_method)
        break
    except:
        break
size_of_file = [
    (f, os.stat(os.path.join(path, f)).st_size)
    for f in files
]
for sub in sub_dir:
    i = os.path.join(path, sub)
    size = 0
    for k in os.listdir(i):
        size += os.stat(os.path.join(i, k)).st_size
    size_of_file.append((sub, size))
for f, s in sorted(size_of_file, key = lambda x: x[1]):
    print("{} : {}MB".format(os.path.join(path, f), round(s/(1024*1024), 3)))

I'm expecting to print the name and file size of all files in the current directory and all the sub-directories.

The documentation has some helpful example code that you might have chosen to follow. A loop forever / next() / break approach could be made to work, I'm sure, but it's not idiomatic and that style does not improve the maintainability of the code.

from pathlib import Path
import os

total = 0
for root, dirs, files in os.walk("."):
    for file in files:
        path = Path(root) / file
        print(path)
        total += path.stat().st_size

print(f"Total of {total} bytes.")

pathlib is amazing here I think, there are many ways of solving this but one simple example is something like this:

from pathlib import Path

dir = "."
paths = list(Path(dir).glob('**/*'))
for path in paths:
    if path.is_file():
        print(f"{path.name}, {path.lstat().st_size}")

You don't need the loop but for simplicity in this example I just used it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM