daniels
daniels

Reputation: 19193

What's the best way to get the size of a folder and all the files inside from python?

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

Answers (4)

Perkins
Perkins

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

Manuel Ceron
Manuel Ceron

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

Martin v. L&#246;wis
Martin v. L&#246;wis

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:

  1. If all the files you want to measure fill a partition, and the partition has no other files, then you can look at the disk usage of the partition.
  2. If you can continuously monitor all files, or are responsible for creating all the files yourself, you can generate an incremental disk usage.

Upvotes: 5

S.Lott
S.Lott

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

Related Questions