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