0

Say I have a PC connected to some experimental instrumentation. C: is the system partition with Windows 7 and all the programs on it, and D: is the storage partition where all the acquired data is saved to. Is there a fast and easy way to generate a rough histogram of the sizes of all files on D:? If possible, I'd like to avoid doing tens of searches for different file sizes and generating a histogram by hand. Some simple tool for this would be very handy.

I've found a tool called WinDirStat, which is nice to visualize all the files, but I couldn't find any feature to get a proper histogram.

1
  • Hi, Nora. Please bring this question to superuser.com where PC-focused questions are asked and answered. Good luck!
    – SamErde
    Oct 10, 2020 at 18:02

2 Answers 2

1

Consider using the following Python script. Works for me both on Windows and Linux with python 3.8 and matplotlib 3.5.

from pathlib import Path
import numpy as np
from matplotlib import pyplot as plt

def file_size_hist(folder: Path, size_min, size_stop, size_step):

    hist_x = np.logspace(start=np.log2(size_min)/np.log2(size_step),
                         stop=np.log2(size_stop)/np.log2(size_step)+1,
                         num=int(np.log2(size_stop/size_min)/np.log2(size_step))+2,
                         base=size_step,
                         dtype=np.uint64)

    hist_y = np.zeros(hist_x.size, dtype=np.uint64)
    for f in folder.rglob('*'):
        if f.is_file():
            file_size_bytes = f.stat().st_size

            if file_size_bytes < size_min:
                bin = 0
            else:
                bin = np.log2(file_size_bytes//size_min) // np.log2(size_step) + 1
            # Example:
            # file_size_bytes = np.array([0, size_min-1, size_min, size_min+1, size_stop-1, size_stop, size_stop+1])
            # bin             = np.array([0,          0,        1,          1,          22,        23,          23])

            bin = int(np.minimum(bin, hist_y.size-1))   # Last bin includes all files with size >= size_stop
            hist_y[bin] += 1

    return hist_x, hist_y


if __name__ == "__main__":
    size_min_bytes = 2**10     # files with size < size_min_bytes are counted in the first bar
    size_stop_bytes = 2**32    # files with size >= size_stop_bytes are counted in the last bar
    size_step_coeff = 2   # e.g. `[1024, 2048, 4096, ...]` bytes
    target_folder = r"F:"

    hist_x, hist_y = file_size_hist(Path(target_folder), size_min_bytes, size_stop_bytes, size_step_coeff)
    print('Found {} files total'.format(np.sum(hist_y)))
    print('Found {} files with size below 64 KB'.format(np.sum(hist_y * (hist_x < 64*2**10))))

    #
    fig, ax = plt.subplots(1, 1)
    ax.hist(hist_x, weights=hist_y, bins=hist_x, label='file sizes')
    ax.set_xscale('log')
    ax.set_yscale('log')
    ax.set_xlabel('File size in bytes')
    ax.set_ylabel('File count')

    fig.show()

    #   |  bin |  min            |               max |           hist_x               |
    # --+------+-----------------+-------------------+--------------------------------+
    #   |   0  |               0 |              1023 |  1024*2**0                     |
    #   |   1  |  1024*2**0=1024 |              2047 |  1024*2**1                     |
    #   |   2  |  1024*2**1=2048 |              4095 |  1024*2**2                     |
    #   |  22  |  1024*2**21     |     1024*2**22-1  |  1024*2**22 == size_stop_bytes |
    #   |  23  |  1024*2**22     |               inf |  1024*2**23                    |

The main benefit is that it is infinitely customizable. Example result: Example file size histogram

0

SpaceSniffer might be what you want

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .