YouTip LogoYouTip

Python3 Os Write

# Python3.x Python3 os.write() Method [![Image 3: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#) * * * ### Overview The os.write() method is used to write a string to a file descriptor fd. It returns the actual number of bytes written. It is valid in Unix. ### Syntax The syntax for the **write()** method is as follows: os.write(fd, str) ### Parameters * **fd** -- The file descriptor. * **str** -- The string to be written. ### Return Value This method returns the actual number of bytes written. ### Example The following example demonstrates the use of the write() method: #!/usr/bin/python3import os, sys # Open a file fd = os.open("f1.txt",os.O_RDWR|os.O_CREAT)# Write a string str = "This is site" ret = os.write(fd,bytes(str, 'UTF-8'))# Print the return valueprint ("The number of bytes written: ")print (ret)print ("Write successful")# Close the file os.close(fd)print ("File closed successfully!!") Executing the above program produces the following output: The number of bytes written: 23Write successfulFile closed successfully!! [![Image 4: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#)
← Python3 Cgi ProgrammingPython3 Os Write β†’