S
S
Salavat Sharapov2014-06-09 10:44:40
Python
Salavat Sharapov, 2014-06-09 10:44:40

How to find out the size of a directory using Python?

In niks, there is a great command du, in order to immediately show the size of subdirectories in a directory.
How can I do this with a Python script?
I do it like this:

import os
#Π£ΠΊΠ°Π·Ρ‹Π²Π°ΡŽ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΡŽ
basedir = "/home/user/test/"
#Пока ΠΏΡ€ΠΈΠΌΠ΅ΠΌ Ρ‡Ρ‚ΠΎ Ρ€Π°Π·ΠΌΠ΅Ρ€ ΠΏΠΎΠ΄Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΉ Ρ€Π°Π²Π΅Π½ 0
sub_size = 0
#Π’Π°ΠΊ ΠΊΠ°ΠΊ ΠΌΡ‹ Π½Π΅ Π·Π½Π°Π΅ΠΌ сколько ΠΏΠΎΠ΄Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΉ Π² Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ, ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΈΡ… названия.
subnames = os.listdir(basedir)
#Π”Π°Π»Π΅Π΅ Ρ‡Π΅Ρ€Π΅Π· Ρ†ΠΈΠΊΠ» сплитим ΠΏΡƒΡ‚ΠΈ Π΄ΠΎ ΠΏΠΎΠ΄Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΉ.
for subname in subnames:
    subpath = "%s/%s" % (os.path.dirname(basedir), subname)
    #ΠŸΠΎΡ‚ΠΎΠΌ Ρ‡Π΅Ρ€Π΅Π· walk, ΠΏΠΎΠ»ΡƒΡ‡ΠΈΠΌ ΠΊΠΎΡ€Ρ‚Π΅ΠΆ (ΠΏΡƒΡ‚ΡŒ Π΄ΠΎ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ, поддирСктория, Ρ„Π°ΠΉΠ»Ρ‹)
    for path, subdirs, files in os.walk(subpath):
         #ΠšΡ‚ΠΎ Ρƒ нас "Сст" мСсто? ΠŸΡ€ΠΎΡ…ΠΎΠ΄ΠΈΠΌΡΡ ΠΏΠΎ этим Ρ„Π°ΠΉΠ»Π°ΠΌ ΠΈ складываСм Ρ€Π°Π·ΠΌΠ΅Ρ€
         for files in file:
              #ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΏΠΎΠ»Π½Ρ‹ΠΉ ΠΏΡƒΡ‚ΡŒ Π΄ΠΎ Ρ„Π°ΠΉΠ»Π°.
              filename = os.path.join(path, file)
              #Π£Π·Π½Π°Π΅ΠΌ Π΅Π³ΠΎ Ρ€Π°Π·ΠΌΠ΅Ρ€ ΠΈ ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅ΠΌ Ρ€Π°Π·ΠΌΠ΅Ρ€ ΠΏΠ°ΠΏΠΊΠΈ
              sub_size += os.path.getsize(filename) /1024 /1024
    print "%s - %.1f Mb" % (basedir[len[basedir]: ], sub_size)

BUT....it gives me the size of the subdirectories..so the size of the second subdirectory is the sum of the sizes of the first and the second, and so on.
PLEASE tell me where is the mistake!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Misha Krinkin, 2014-06-09
@desperadik

If I understand correctly, then you need to move sub_size = 0:

import os

basedir = "/home/user/test/"

subnames = os.listdir(basedir)
for subname in subnames:
    sub_size = 0
    subpath = "%s/%s" % (os.path.dirname(basedir), subname)
    for path, subdirs, files in os.walk(subpath):
         for files in file:
              filename = os.path.join(path, file)
              sub_size += os.path.getsize(filename) /1024 /1024
    print "%s - %.1f Mb" % (basedir[len[basedir]: ], sub_size)

V
vladimir-klp, 2022-03-12
@vladimir-klp

Also studied this issue. There are many solutions, many cumbersome. As a result, I wrote a short function that calculates the size of the directory to a byte, the number of files in the directory (checked with the value from the folder property in Windows)

import os
from pathlib import Path

#ВычисляСт Ρ€Π°Π·ΠΌΠ΅Ρ€ ΠΏΠ°ΠΏΠΊΠΈ, количСство Ρ„Π°ΠΉΠ»ΠΎΠ² ΠΈ количСство ΠΈΡ‚Π΅Ρ€Π°Ρ†ΠΈΠΉ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ
def folderSize(path):
    fsize = 0
    numfile = 0
    iteration = 0
    for file in Path(path).rglob('*'):
        if (os.path.isfile(file)):
            fsize += os.path.getsize(file)
            numfile +=1
        iteration+=1
    return fsize, numfile, iteration

Next, in the code, we call it (the piece is taken from my working script):
print("ВычислСниС Ρ€Π°Π·ΠΌΠ΅Ρ€Π° Π²Ρ‹Π±Ρ€Π°Π½Π½ΠΎΠΉ ΠΏΠ°ΠΏΠΊΠΈ...")
size, numfile, iteration = folderSize(folder)
print(f'Π’Ρ‹Π±Ρ€Π°Π½Π° ΠΏΠ°ΠΏΠΊΠ°: {folder}')
print(f'НайдСно Ρ„Π°ΠΉΠ»ΠΎΠ²: {numfile}')
print("Π Π°Π·ΠΌΠ΅Ρ€ ΠΏΠ°ΠΏΠΊΠΈ:")
print(f'{size} Bytes')       
print(f'{size/1048576:.2f} Mb')
print(f'{size/1073741824:.2f} Gb')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question