Pythonでファイルを操作するread()メソッドの使用チュートリアル
888 ワード
read()メソッドは、ファイルsizeバイトサイズを読み出します.読み込み中にEOFサイズバイトが取得されるまでは、使用可能なバイトしか読み込めません.構文
read()メソッドの構文は、次のとおりです.
パラメータ size--ファイルから読み込めるバイト数です.
戻り値
このメソッドは、読み取り文字列のバイト数を返します.例
次の例ではread()メソッドの使用を示します.
上記のプログラムを実行すると、次の結果が得られます.
read()メソッドの構文は、次のとおりです.
fileObject.read( size );
パラメータ
戻り値
このメソッドは、読み取り文字列のバイト数を返します.例
次の例ではread()メソッドの使用を示します.
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name
# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
line = fo.read(10)
print "Read Line: %s" % (line)
# Close opend file
fo.close()
上記のプログラムを実行すると、次の結果が得られます.
Name of the file: foo.txt
Read Line: This is 1s