こもろぐ @tenkoma

What We Find Changes Who We Become -- Peter Morville著『アンビエント・ファインダビリティ 』

広告:本ブログで紹介している書籍等商品の紹介でAmazonアソシエイトを利用していることがあります。

あるディレクトリ以下のファイルとフォルダを再帰的に表示する(初めてのPython p.586)

import os,stat,sys
def reprDirInfo(dirpath, indent=0):
    print " "*(indent-1),"+",dirpath
    for path in os.listdir(dirpath):
        full = os.path.join(dirpath,path)
        if os.path.isdir(full):
            reprDirInfo(full,indent+4)
        elif os.path.isfile(full):
            print " "*(indent+3),"-",full,"\t",os.stat(full).st_size
    print ""

if __name__ == '__main__':
    for path in sys.argv[1:]:
        reprDirInfo(path)
        print "-"*60

sys.argvを参照するところでスライシングをインデクシングと間違えて20分間格闘する.