Python| os.writev() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.writev()
方法用于将指定缓冲区的内容写入指定的文件描述符。在这里,缓冲区是可变字节类对象的序列。缓冲区按指定的顺序处理。第一个缓冲区的全部内容在进入第二个缓冲区之前被写入,依此类推。
文件描述符是与当前进程已打开的文件相对应的小整数值。它用于执行各种较低级别的 I/O 操作,如读、写、发送等。
注意: os.writev()
方法仅适用于 UNIX 平台。
Syntax: os.writev(fd, buffers)
Parameters:
fd: A file descriptor which is to be written.
buffers: A sequence of mutable bytes-like objects containing the data to be written to the specified file descriptor.
Return Type: This method returns an integer value which represents the number of bytes actually written.
代码:使用 os.writev() 方法将缓冲区的内容写入文件
# Python program to explain os.writev() method
# import os module
import os
# File path
path = "./file2.txt"
# Create a file and get the
# file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_CREAT | os.O_WRONLY)
# Bytes-like objects
# the data to be written in the file
buffer1 = bytearray(b"GeeksForGeeks: ")
buffer2 = bytearray(b"A computer science portal ")
buffer3 = bytearray(b"for geeks")
# write the data contained in
# bytes-like objects
# to the file descriptor fd
# using os.writev() method
numBytes = os.writev(fd, [buffer1, buffer2, buffer3])
# print the content of file
with open(path) as f:
print(f.read())
# Print the number of bytes actually written
print("Total Number of bytes actually written:", numBytes)
输出:
GeeksForGeeks: A computer science portal for geeks
Total Number of bytes actually written: 50