Re: [Tutor] Getting "file sizes"
magnus at thinkware.se
Thu Feb 19 04:02:38 EST 2004
R. Alan Monroe wrote:
> This isn't recursive, but for a single directory it would work:
> [(x,os.path.getsize(x)) for x in os.listdir('c:/')]
Only if 'c:/' happens to be your current working directory...
The problem is that os.listdir just returns file names, not
full paths. os.path.getsize(x) won't find x if it's just the
name of a file located in another directory than the current.
If you would for instance replace os.listdir('c:/') with
glob.glob('c:/*') you would get full paths that make
os.path.getsize happy. You could also do
dir = 'c:/'
print [(x,os.path.getsize(dir+x)) for x in os.listdir(dir)]
For a recursive version, try os.walk.
for root, dirs, files in os.walk('d:/nyponverktyg'):
for file in files:
path = os.path.join(root, file)
print path, os.path.getsize(path)
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus at thinkware.se
More information about the Tutor