Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
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)
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
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 questionAsk a Question
731 491 924 answers to any question