glob

python 3.4からは再帰的に検索することが可能になった.たとえば,

glob.glob(os.path.join(base_dir,'**','*.nc'),recursive=True)

でbase_dir (base directory)以下の全ての"*.nc"ファイルを検索することができる.ただしかなり遅いようなので,linux commandを呼び出す方法で,find を使う方が良いかもしれない.

実際によく使う方法は,入力データがindirというディレクトリにあるとすると

for input_file in glob.glob(indir + '*.nc'):

....

でinput_fileにファイル名をfull pathで入れることだ.ファイルが見つからなければループがスキップされる

これは非常に有用なパッケージだ.基本的な機能はglob.glob(ファイル名(ワイルドカード使用可能))で,ファイル名を,full pathで返すことである.戻り値は,文字列のリストになる.ファイルが見つからなければ,リストの長さ(len(リスト名))が0になる.

This is a very useful package. Assuming that the input files are in the directory of "indir", you can use this package as

for input_file in glob.glob(indir + '*.nc'):

....

Then, "input_file" has the input file name (full path).

In order to use this package efficiently, it is important to put files that are processed by one script in one directory.

So, desgin of directories are very important.