无涯教程-OS File - os.write(fd, str)函数

67 阅读1分钟

Python方法write()将字符串str写入文件描述符fd。返回实际写入的字节数。

os.write(fd, str) - 语法

os.write(fd, str)
  • fd    -  这是文件描述符。

  • str   -  这是要写入的字符串。

os.write(fd, str) - 返回值

此方法返回实际写入的字节数。

os.write(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!!

参考链接

www.learnfk.com/python/os-w…