Latest web development tutorials

Python3 os.write () method

Python3 OS file / directory methods Python3 OS file / directory methods


Outline

os.write () method is used to write strings to the file descriptor fd in. returns the actual length of the string is written.

Effective on Unix.

grammar

write () method syntax is as follows:

os.write(fd, str)

parameter

  • fd - file descriptor.

  • str - the string written.

return value

This method returns the actual number of bits written.

Examples

The following example demonstrates the write () method of use:

#!/usr/bin/python3

import os, sys

# 打开文件
fd = os.open("f1.txt",os.O_RDWR|os.O_CREAT)

# 写入字符串
str = "This is w3big.com site"
ret = os.write(fd,bytes(str, 'UTF-8'))

# 输入返回值
print ("写入的位数为: ")
print (ret)

print ("写入成功")

# 关闭文件
os.close(fd)
print ("关闭文件成功!!")

The above program output is:

写入的位数为: 
23
写入成功
关闭文件成功!!

Python3 OS file / directory methods Python3 OS file / directory methods