Reputation: 19193
If there will be a small number of files it should be easy with a recursive function to pass through all the files and add the size but what if there are lots of files, and by lots i really mean lots of files.
Upvotes: 6
Views: 3969
Reputation: 2517
If you are on a posix system that provides du, you should be able to use pexpect.run
or subprocess
to execute du <path to folder>
to get the size of the folder and subfiles. Keep in mind it will output a string with each file listed and each folder totaled. (Look at the du manpage to see what you can do to limit that).
Upvotes: 0
Reputation: 8456
There is a recipe for that problem in the Python Cookbook (O'Reilly). You can read the full solution with an example online:
http://safari.oreilly.com/0596001673/pythoncook-CHP-4-SECT-24
or here:
http://books.google.com/books?id=yhfdQgq8JF4C&pg=PA152&dq=du+command+in+python
Upvotes: 1
Reputation: 127447
There is no other way to compute the size than recursively invoking stat. This is independent of Python; the operating system just provides no other way.
The algorithm doesn't have to be recursive; you can use os.walk.
There might be two exceptions to make it more efficient:
Upvotes: 5
Reputation: 391818
You mean something like this?
import os
for path, dirs, files in os.walk( root ):
for f in files:
print path, f, os.path.getsize( os.path.join( path, f ) )
Upvotes: 10