How to determine the folder in a directory – size

How to determine the folder in a directory – size

It is a Python script for a Linux operating system:

import os
import subprocess

# determine the folder which you want to scan
parent_path = '/home/sven/Musik'

print(f"Größen der Unterverzeichnisse in {parent_path}:")
print("=" * 40)

# Serach for all folder only on the first folder not subdirectories
for entry in os.scandir(parent_path):
    if entry.is_dir():
        # FRun ‘du -sh’ for each folder
        # split()[0] takes the largest part of the output
        try:
            size_output = subprocess.check_output(['du', '-sh', entry.path]).split()[0].decode('utf-8')
            print(f"{entry.name}: {size_output}")
        except subprocess.CalledProcessError as e:
            # Handles errors, e.g. when access to a folder is denied
            print(f"Fehler bei der Grössenbestimmung von {entry.name}: {e}")

The results are (yes the folders haven the German language, because I live in Germany, and I’m German – sorry for that)

Größen der Unterverzeichnisse in /home/sven/Musik

1980-1989: 6,8G
Musikvideo: 12M
1970-1979: 3,2G
sonstige_Musik: 6,7G
1990-1999: 121M
Podcast: 5,8G
1960-1969: 2,7G
Weihnachten: 921M
2000-2009: 8,5G
2010-2019: 1,7G
Hörbücher: 92M
Klassik: 1,5G
Musikalben: 3,5G
erster_Versuch: 136K
1950-1959: 95M

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

error: Content is protected !!