Python3-os-write
提供:Dev Guides
Python 3-os.write()メソッド
説明
メソッド* write()は、文字列 *str をファイル記述子 fd に書き込みます。 実際に書き込まれたバイト数を返します。
構文
以下は、* write()*メソッドの構文です-
os.write(fd, str)
パラメーター
- fd -これはファイル記述子です。
- str -これは書き込まれる文字列です。
戻り値
このメソッドは、実際に書き込まれたバイト数を返します。
例
次の例は、write()メソッドの使用法を示しています。
# !/usr/bin/python3
import os, sys
# Open a file
fd = os.open( "f1.txt", os.O_RDWR|os.O_CREAT )
# Write one string
line = "this is test"
# string needs to be converted byte object
b = str.encode(line)
ret = os.write(fd, b)
# ret consists of number of bytes written to f1.txt
print ("the number of bytes written: ", ret)
# Close opened file
os.close( fd)
print ("Closed the file successfully!!")
結果
上記のプログラムを実行すると、次の結果が生成されます-
the number of bytes written: 12
Closed the file successfully!!