os.write() Method
Python3 os.write() method is used to write a string to the file descriptor fd. Returns the length of the actual string written. Valid in Unix.
Python os.write() Syntax
The syntax format of the write() method is as follows.
os.write(fd, str)
Python os.write() Parameters
- fd – The file descriptor.
- str – The string to write.
Python os.write() Return Value
This method returns the actual number of bits written.
Python os.write() Example#1
The following example demonstrates the use of the write() method.
#!/usr/bin/python3
import os, sys
# Open File
fd = os.open("apidemos.txt",os.O_RDWR|os.O_CREAT)
# Write string
str = "This is apidemos.com site"
ret = os.write(fd,bytes(str, 'UTF-8'))
# Enter the return value
print ("The number of bits written in is: ")
print (ret)
print ("Write success.")
# Close file
os.close(fd)
print ("Close file successfully!!!")
Output: