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!!
上一篇:
Python3檔方法
下一篇:
Python3 os檔目錄的方法