OS File 中的 os.write(fd, str)函数

首页 / Python2入门教程 / OS File 中的 os.write(fd, str)函数

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!!

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

赵成的运维体系管理课 -〔赵成〕

技术管理实战36讲 -〔刘建国〕

高并发系统设计40问 -〔唐扬〕

图解 Google V8 -〔李兵〕

高楼的性能工程实战课 -〔高楼〕

李智慧 · 高并发架构实战课 -〔李智慧〕

Kubernetes入门实战课 -〔罗剑锋〕

JavaScript进阶实战课 -〔石川〕

结构写作力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)