Python中的OS模块提供了与操作系统进行交互的功能。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的功能的便携式方法。
os.writev()Python中的方法用于将指定缓冲区的内容写入指定的文件描述符。在这里,缓冲区是可变bytes-like对象的序列。缓冲区以指定顺序处理。在进入第二缓冲区之前,先写入第一缓冲区的全部内容,依此类推。
文件描述符是一个小整数值,对应于当前进程已打开的文件。它用于执行各种较低级别的I /O操作,例如读取,写入,发送等。
注意:os.writev()该方法仅在UNIX平台上可用。
用法: os.writev(fd, buffers)
参数:
fd:要写入的文件描述符。
buffers:一系列可变的bytes-like对象,其中包含要写入指定文件描述符的数据。
返回类型:此方法返回一个整数值,该值表示实际写入的字节数。
代码:使用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