Esercizio 3 Prova Pratica 29-05-14

From Sistemi Operativi
Revision as of 18:06, 26 April 2015 by Davide.crestini (talk | contribs) (Created page with "<source lang="text"> Il comando che dovrete implementare come script shell o programma python e' statlen. Data una directory statlen fa una statistica sulla lunghezza dei nomi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Il comando che dovrete implementare come script shell o programma python e' statlen.
Data una directory statlen fa una statistica sulla lunghezza dei nomi dei file presenti in tutto il sottoalbero con radice nella
directory passata come parametro.
es.
$ statlen /tmp
2: 2
3: 10
5: 4
...
significa che in tmp (e in tutte le sottodir.ectory di /tmp) ci sono 2 file con nome di due caratteri, 10 con nomi di 3 caratteri e
cosi' via.

Soluzione di Krusty

import os, sys from stat import *

def recScan(pathname,v): for f in os.listdir(pathname): path = os.path.join(pathname,f) s = os.stat(path) if S_ISREG(s.st_mode): l = len(f) if(l not in v): v[l]=1 else: v[l] = v[l] + 1 elif S_ISDIR(s.st_mode): recScan(path,v)

v = {} recScan(sys.argv[1],v) for key in v: print(key, ':', v[key])