Python3 os.write() Method
Python3 OS File/Directory Methods
Overview
The os.write() method writes a string to the file descriptor fd and returns the actual length of the string written.
Valid on Unix.
Syntax
The **write()** method syntax is as follows:
os.write(fd, str)
Parameters
-
fd -- The file descriptor.
-
str -- The string to write.
Return Value
This method returns the actual number of bytes written.
Example
The following example shows how to use the write() method:
#!/usr/bin/python3
import os, sys
# Open file
fd = os.open("f1.txt",os.O_RDWR|os.O_CREAT)
# Write string
str = "This is runoob.com site"
ret = os.write(fd,bytes(str, 'UTF-8'))
# Input return value
print ("写入的位数为: ")
print (ret)
print ("写入成功")
# Close file
os.close(fd)
print ("关闭文件成功!!")
The output of the above program is:
Number of bits written:
23
写入成功
关闭文件成功!!