0

I have written a python script where I am trying to find the size of a sub-directory where I can only define the base path of the directory and need to traverse for the folder whose size I want to check. I am able to print that particular directory but size of that directory is showing O MB while it is 31MB folder. What could be I missing. Please help.

# determine size of a given folder in MBytes
import os
# pick base path you have ...
for root, dirs, files in os.walk('E:\pkg'):
    Total_size = 0
    for dir in dirs:
        if dir.startswith('mobile'):
            dir = os.path.join(root, dir)
            Total_size = os.path.getsize(dir)
            print(dir)
            print("Size = %0.1f MB" % (Total_size / (1024 * 1024.0)))
winx
  • 13
  • 7
  • 1
    Have u tried printing ``Total_size`` & see what's the value ? – sushanth Jun 20 '20 at 10:39
  • When you say that a directory has a size of 31MB what you're really saying is that the files nested under that directory have a total size of 31MB (the directory and any subdirectories have negligible size themselves). But your code isn't looking at any of the files. And using `os.path.getsize()` won't help: if given a directory path, it will report only the size of the directory itself, not the files inside of it. – FMc Jun 20 '20 at 10:43
  • so how can I fix that @FMc – winx Jun 20 '20 at 10:45
  • @winx Look at the `files`, get their sizes, add it all up. Search StackOverflow: https://stackoverflow.com/questions/1392413/calculating-a-directorys-size-using-python – FMc Jun 20 '20 at 10:46
  • how to fix that @Sushanth – winx Jun 20 '20 at 10:47
  • I guess this can help you: https://stackoverflow.com/q/1392413/13775029 – Krzysztof_K Jun 20 '20 at 11:02
  • Does this answer your question? [Calculating a directory's size using Python?](https://stackoverflow.com/questions/1392413/calculating-a-directorys-size-using-python) – norok2 Jun 20 '20 at 11:34

1 Answers1

2

.getsize does not work on a directory. It will give you the size of a file, not a directory - which is why you're getting 0 for everything.

The method to calculate a directory's size is to walk through all the subdirectories recursively keep summing the sizes of each file.

Or at least, that's what you would do, before we had pathlib

root_dir = Path("path/to/directory")
root_dir_size = sum([f.stat().st_size for f in root_dir.glob('**/*') if f.is_file()])

This will result in root_dir_size in bytes, you can then process that however you want.

Why do it in a billion lines when a single line is enough?

Edit: It's really easy to work with any kind of directories, full path or not - but I suppose I should clear that up too since OP asked.

Change this-

dir = os.path.join(root, dir)
Total_size = os.path.getsize(dir)

to this-

root_subdir = Path(os.path.join(root, dir))
Total_size = sum([f.stat().st_size for f in root_subdir.glob('**/*') if f.is_file()])
Chase
  • 5,315
  • 2
  • 15
  • 41
  • But I cant define my full path. I need to walk to that particular base path and then look for that directory @Chase – winx Jun 20 '20 at 11:20
  • @winx `Path` supports relative paths too, use `os.path` for extra flexibility – Chase Jun 20 '20 at 11:27
  • @winx Welcome to stackoverflow! If you thought this post answers your question, consider marking this as the answer by clicking on the tick next to the votes, this will help other people searching for this question in the future – Chase Jun 22 '20 at 05:22