YouTip LogoYouTip

Os Write

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