FileSystem(ファイルシステム)

# open は file object を返す fp = open( "d:/test.txt" , 'w') print f fpW = open('d:/test.txt', 'w') fpW.write("test"); # stdin から入力 a = raw_input();



tmpfile

tmpfile を生成するには tempfile モジュールを使う。
import tempfile path = tempfile.mktemp() # / に変更する path = tempfile.mktemp().replace('\\', '/')



os

directory を再帰的に作成する場合は, os.makedirs() を使う
import os os.makedirs( "d:/foo/bar/goo" )
# カレントディレクトリの変更 os.chdir( "d:/" ) # カレントディレクトリ os.getcwd()
外部プロセスの実行
os.system( "notepad" )
import os os.mkdir("d:/py/" ); os.mkdir("d:/foo/bar" ); # test.txt os.path.basename( "d:/test.txt" ); # test.txt ( filename が返る ) os.path.basename( "d:/test.txt" ); # ( test, txt ) file, ext = os.path.splitext( "test.txt" ) # d:/foo os.path.dirname( "d:/foo/test.txt" ); # True os.path.isdir( "c:/" ); # ディレクトリ存在チェック os.path.exists( "d:/foobar" ); # ファイル存在チェック os.path.exists( "d:/test.txt" ); # os.path.join # ディレクトリ内のエントリをリストとして返す os.listdir( "d:/" )



shutil

ファイルのコピーなどの操作は shutil モジュールを使う。
import shutil shutil.copy( "d:/src.txt", "d:/dst.txt" ) shutil.move( "d:/src.txt", "d:/dst.txt" )



getFile

os.walk( パス )と渡すと パス以下のディレクトリから、パス/サブディレクトリ/ファイル名のタプルを返す。 再帰処理をしなくても自動ですべてのファイルをイテレートする。
import os for root, dirs, files in os.walk( "c:/" ): for f in files: print root, f



BuildPath

import os # d:\test\foo\bar os.path.join( "d:\\test", "foo", "bar" );



glob.getFile

ディレクトリからファイルのリストを取得するには glob を使う。 ディレクトリをワイルドカード検索してリストを返す
import glob print glob.glob( "*.py" );
ディレクトリを含めたパターン指定もできる。
print glob.glob( "c:/users/*/*.py" );