Python3 os.write() Method

Python3 OS 文件/目录方法Python3 OS File/Directory Methods


Overview

The os.write() method is used to write a string to the file descriptor fd. It returns the actual length of the string written.

Valid on Unix.

Syntax

write()The method syntax 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 bytes written.

Examples

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

#!/usr/bin/python3

import os, sys

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

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

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

print ("写入成功")

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

The output of the above program is:

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

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions