Python方法write()将字符串str写入文件描述符fd。返回实际写入的字节数。
os.write(fd, str)
fd - 这是文件描述符。
str - 这是要写入的字符串。
此方法返回实际写入的字节数。
Python下面的示例展示了write()方法的用法。
# !/usr/bin/python import os, sys # 打开文件 fd=os.open("f1.txt",os.O_RDWR|os.CREAT) # 写入内容 ret=os.write(fd,"This is test") # ret 包含写入 f1.txt 的字节数 print "the number of bytes written: " print ret print "written successfully" # 关闭打开的文件 os.close(fd) print "Closed the file successfully!!"
当无涯教程运行上面的程序时,它产生以下输出-
the number of bytes written: 12 written successfully Closed the file successfully!!
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
HarmonyOS快速入门与实战 -〔QCon+案例研习社〕