Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This grabs subdirectories:</p> <pre><code>import os def get_size(start_path = '.'): total_size = 0 for dirpath, dirnames, filenames in os.walk(start_path): for f in filenames: fp = os.path.join(dirpath, f) total_size += os.path.getsize(fp) return total_size print get_size() </code></pre> <p>And a oneliner for fun using <a href="http://docs.python.org/library/os.html?highlight=shutil#os.listdir" rel="nofollow noreferrer">os.listdir</a> (<em>Does not include sub-directories</em>):</p> <pre><code>import os sum(os.path.getsize(f) for f in os.listdir('.') if os.path.isfile(f)) </code></pre> <p>Reference:</p> <p><a href="http://docs.python.org/library/os.path.html#os.path.getsize" rel="nofollow noreferrer">os.path.getsize</a> - Gives the size in bytes</p> <p><a href="http://docs.python.org/library/os.html?highlight=shutil#os.walk" rel="nofollow noreferrer">os.walk</a></p> <p><strong>Updated</strong> To use <em>os.path.getsize</em>, this is clearer than using the os.stat().st_size method.</p> <p><em>Thanks to ghostdog74 for pointing this out!</em></p> <p><a href="http://docs.python.org/library/os.html?highlight=shutil#os.stat" rel="nofollow noreferrer">os.stat</a> - <em>st_size</em> Gives the size in bytes. Can also be used to get file size and other file related information.</p> <p><strong>Update 2018</strong></p> <p>If you use Python 3.4 or previous then you may consider using the more efficient <code>walk</code> method provided by the third-party <a href="https://pypi.org/project/scandir/" rel="nofollow noreferrer"><code>scandir</code></a> package. In Python 3.5 and later, this package has been incorporated into the standard library and <code>os.walk</code> has received the corresponding increase in performance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload