Dup Ver Goto 📝

GetDiskUsage

PT2/lang/python/fs does not exist
To
43 lines, 147 words, 957 chars Page 'GetDiskUsage' does not exist.

This is a simple example of using shutil. The point is to make a list of files that can be copied to equal-sized volumes with a minimum allocation unit (e.g. FAT cluster size). It then outputs a list of files for each disk. Just manually edit the glob("*") line to get the list of all files you want to copy.

#!/usr/bin/env python3
from glob import glob
import os
import shutil
import sys
import math
args = sys.argv[1:]

cluster_size = 4096
fns = glob("*")

target = args[0]

du = shutil.disk_usage(target)
fr = du.free
fcs = int( du.free / cluster_size )

total = 0
disks = []
disks.append([])
for fn in fns:
  sz = os.path.getsize(fn)
  cs = math.ceil(sz/cluster_size)
  total += cs
  if total > fcs:
    disks.append([fn])
    total = cs
  else:
    disks[-1].append(fn)

for i,disk in enumerate(disks):
  print("Disk",i+1)
  for x in disk:
    print(f"  {x}")
  with open(f"disk_{i:02d}.txt","wt") as f:
    print("\n".join(disk),file=f)