Quick notes on file management in Python

April 10, 2014
By

(This article was originally published at Stat Bandit, and syndicated at StatsBlogs.)

This is primarily for my recollection

To expand ~ in a path name:

os.path.expanduser('~')

To get the size of a directory:

import os
def getsize(start<em>path = '.'):
    totalsize = 0
    for dirpath, dirnames, filenames in os.walk(start</em>path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            totalsize += os.path.getsize(fp)
    return totalsize



Please comment on the article here: Stat Bandit

Tags:


Subscribe

Email:

  Subscribe