Python-os-write

提供:Dev Guides
移動先:案内検索

Python os.write()メソッド

説明

Pythonメソッド* write()*は、文字列_str_をファイル記述子_fd_に書き込みます。 実際に書き込まれたバイト数を返します。

構文

以下は、* write()*メソッドの構文です-

os.write(fd, str)

パラメーター

  • fd -これはファイル記述子です。
  • str -これは書き込まれる文字列です。

戻り値

このメソッドは、実際に書き込まれたバイト数を返します。

Pythonの次の例は、write()メソッドの使用法を示しています。

# !/usr/bin/python

import os, sys

# Open file
fd = os.open("f1.txt",os.O_RDWR|os.CREAT)

# Writing text
ret = os.write(fd,"This is test")

# ret consists of number of bytes written to f1.txt
print "the number of bytes written: "
print  ret

print "written successfully"

# Close opened file
os.close(fd)
print "Closed the file successfully!!"

上記のプログラムを実行すると、次の結果が生成されます-

the number of bytes written:
12
written successfully
Closed the file successfully!!