0

Following is the code

import os
def get_size(path):
    total_size = 0
    for root, dirs, files in os.walk(path):
        for f in files:
            fp = os.path.join(root, f)
            total_size += os.path.getsize(fp)
    return total_size

for root,dirs,files in os.walk('F:\House'):
   print(get_size(dirs))

OUTPUT :

F:\House 21791204366
F:\House\house md 1832264906
F:\House\house md\house M D 1 1101710538
F:\House\Season 2 3035002265
F:\House\Season 3 3024588888
F:\House\Season 4 2028970391
F:\House\Season 5 3063415301
F:\House\Season 6 2664657424
F:\House\Season 7 3322229429
F:\House\Season 8 2820075762

I need only sub directories after main directory with their sizes. My code is going till the last directory and writing its size.

As an example:

F:\House 21791204366
F:\House\house md 1832264906
F:\House\house md\house M D 1 1101710538

It has printed the size for house md as well as house M D 1 (which is a subdirectory in house md). But I only want it till house md sub directory level.

DESIRED OUTPUT: I need the size of each sub dir after the main dir level (specified by the user) and not sub sub dir (but their size should be included in parent dirs.)

How do I go about it ?

jfs
  • 399,953
  • 195
  • 994
  • 1,670
Sushant
  • 160
  • 2
  • 10
  • I don't understand what you're asking for. Your example output has `F:\House\house md\house M D 1` but then you say you don't want it. What is the desired output? – sco1 Feb 06 '18 at 17:51
  • Desired output >> I want the size of each sub dir after the main dir level (specified by the user) and not sub sub dir (but their size should be included in parent dirs.) – Sushant Feb 06 '18 at 17:54
  • Please [edit] your question to include the desired output. – sco1 Feb 06 '18 at 17:55

2 Answers2

0

Instead of using os.walk in your getpath function, you can use listdir in conjunction with isdir:

for file in os.listdir(path):
    if not os.path.isdir(file):
        # Do your stuff
        total_size += os.path.getsize(fp)

        ...

os.walk will visit the entire directory tree, whereas listdir will only visit the files in the current directory.

However, be aware that this will not add the size of the subdirectories to the directory size. So if "Season 1" has 5 files of 100MB each, and 5 directories of 100 MB each, then the size reported by your function will be 500MB only.

Hint: Use recursion if you want the size of subdirectories to get added as well.

havanagrawal
  • 1,039
  • 6
  • 12
0

To print the size of each immediate subdirectory and the total size for the parent directory similar to du -bcs */ command:

#!/usr/bin/env python3.6
"""Usage: du-bcs <parent-dir>"""
import os
import sys

if len(sys.argv) != 2:
    sys.exit(__doc__)  # print usage

parent_dir = sys.argv[1]
total = 0
for entry in os.scandir(parent_dir):
    if entry.is_dir(follow_symlinks=False): # directory
        size = get_tree_size_scandir(entry)
        # print the size of each immediate subdirectory
        print(size, entry.name, sep='\t')  
    elif entry.is_file(follow_symlinks=False): # regular file
        size = entry.stat(follow_symlinks=False).st_size
    else:
        continue
    total += size
print(total, parent_dir, sep='\t') # print the total size for the parent dir

where get_tree_size_scandir()[text in Russian, code in Python, C, C++, bash].

The size of a directory here is the apparent size of all regular files in it and its subdirectories recursively. It doesn't count the size for the directory entries themselves or the actual disk usage for the files. Related: why is the output of du often so different from du -b.

jfs
  • 399,953
  • 195
  • 994
  • 1,670