python os写文本_Python os.writev()用法及代码示例

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值