2015年8月27日木曜日

Python 少しだけ実用的な所に触れてく(2)

■関数、ファイルサイズ取得

ファイルサイズを表示する関数を書いてみる
 >>> import os
 >>> import glob
 >>>
 >>> # 関数
 >>> def GetFileSize(target_file):
 ...     fsize = os.path.getsize(target_file)
 ...     print target_file, "=", fsize
 ...
 >>>
 >>> # 対象の指定
 >>> target = r'd:\tmp\*.*'
 >>>
 >>> # 対象についてファイルサイズを表示(関数呼び出し)
 >>> for file in glob.glob(target):
 ...     GetFileSize(file)
 ...
 →「ファイル名 = ファイルサイズ」の形で列挙されます

0 件のコメント:

コメントを投稿