Hi.
I wanted to write a small script relating to the capacity information of an SD card, flash drive, etc …
I have something like this but it doesn’t show me the correct size.
import xbmc, xbmcaddon, xbmcgui, xbmcplugin, os, sys, xbmcvfs
import re
import time
try:
translatePath = xbmcvfs.translatePath
except AttributeError:
translatePath = xbmc.translatePath
storage = translatePath(os.path.join('/storage'))
total_size12 = 0
BytesPerGiB = 1024 * 1024 * 1024
for dirpath12, dirnames12, filenames12 in os.walk(storage):
for f12 in filenames12:
fp12 = os.path.join(dirpath12, f12)
total_size12 += os.path.getsize(fp12)
total_sizetext12 = "%.2f GiB" % (total_size12/BytesPerGiB)
xbmcgui.Window(10000).setProperty('total_sizetext12', "[B]Storage: " + total_sizetext12 + "[/B]")
I don’t know how to do - free, total
would anyone be able to suggest how to do it?
thanks
I only have c-snippes for you but you can translate it to script/pyhton:
// get full size of block device
uint64_t get_block_dev_size(char *dev)
{
FILE *filp;
uint64_t number = 0;
snprintf(cbuffer, BUFFER_SZ, "/sys/block/%s/size", dev);
if (!file_exist(cbuffer)) {
printf("Parameter for block device '%s' not found: '%s'\n", MMC_BLOCK_DEVICE, cbuffer);
return 0;
}
filp = fopen(cbuffer, "r");
if (filp != NULL) {
if (fgets(cbuffer, BUFFER_SZ, filp) != NULL)
number = (uint64_t)strtoll(cbuffer, NULL, 10) * MMC_BLOCK_SIZE;
fclose(filp);
}
return number;
}
MMC_BLOCK_SIZE == 512
MMC_BLOCK_DEVICE == mmcblk0 for eMMC, mmcblk1 for uSD
// get full size of eMMC and show a "user friendly" GB size of the eMMC
uint64_t emmc_size = get_block_dev_size(MMC_BLOCK_DEVICE);
printf("\neMMC size: 0x%012llx [%dGB]\n",
(long long unsigned int) emmc_size,
next_pow2(emmc_size / (1000 * 1000 * 1000)));
1 Like
thanks i’ll try to do something about it, although i doubt if i can manage it
import shutil
folder = '/storage'
total_mem, used_mem, free_mem = shutil.disk_usage(folder)
gb = 1024 * 1024 * 1024
print('Folder {}' . format(folder))
print(' total: {:.1f} GB' . format(total_mem / gb))
print(' used : {:.1f} GB' . format(used_mem / gb))
print(' free : {:.1f} GB' . format(free_mem / gb))
.
vim3: # python disk_usage.py
Folder /storage
total: 10.5 GB
used : 3.6 GB
free : 6.8 GB
.
vim3: # df -h
/dev/CE_STORAGE 10.5G 3.6G 6.8G 35% /storage
1 Like
Thanks , @Portisch ,@vpeter
I managed to do something like that
i can’t get the correct format
tried
BytesPerGiB = 1024 * 1024 * 1024
%.0f GiB
%.2f GiB
free_mem/1024000.0
free_mem/gb
free_mem/BytesPerGiB
import shutil
folder = '/storage'
total_mem, used_mem, free_mem = shutil.disk_usage(folder)
print('Folder {}' . format(folder))
total_sizeTM = "%.0f GiB" % (total_mem/BytesPerGiB)
total_sizeUM = "%.0f GiB" % (used_mem/1024000.0)
free_memFM = "%.0f GiB" % (free_mem/1024000.0)
xbmcgui.Window(10000).setProperty('str(free_mem)', str(free_mem))
xbmcgui.Window(10000).setProperty('str(total_mem)', str(total_mem))
xbmcgui.Window(10000).setProperty('str(used_mem)', str(used_mem))
<label>total_mem: $INFO[Window().Property(str(total_mem))][CR]used_mem: $INFO[Window().Property(str(used_mem))][CR]free_mem: $INFO[Window().Property(str(free_mem))]</label>
for block devices it’s most 1000, not 1024
thanks, I had something else mixed up, both work properly,
BytesPerGiB = 1000 * 1000 * 1000
gb = 1024 * 1024 * 1024
I have one more question, is there any autodetect pendrive to do
something like
var / media / *
thank you very much for your help and hints !
Classic example of over complicating things.
#/bin/bash
df -h /storage
On our research we found out df
do deliver wrong values. That’s why we use rsync
.
system
Closed
20 October 2021 11:13
10
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.