Python3 os.write() 方法

Python 3 教程 · 2019-02-07 21:58:48

概述

os.write() 方法用于写入字符串到文件描述符 fd 中. 返回实际写入的字符串长度。

在Unix中有效。

语法

write()方法语法格式如下:

os.write(fd, str)

参数

  • fd -- 文件描述符。

  • str -- 写入的字符串。

返回值

该方法返回写入的实际位数。

实例

以下实例演示了 write() 方法的使用:

#!/usr/bin/python3

import os, sys

# 打开文件
fd = os.open("f1.txt",os.O_RDWR|os.O_CREAT)

# 写入字符串
str = "This is codercto.com site"
ret = os.write(fd,bytes(str, 'UTF-8'))

# 输入返回值
print ("写入的位数为: ")
print (ret)

print ("写入成功")

# 关闭文件
os.close(fd)
print ("关闭文件成功!!")

执行以上程序输出结果为:

写入的位数为: 
23
写入成功
关闭文件成功!!

点击查看所有 Python 3 教程 文章: https://codercto.com/courses/l/10.html

查看所有标签

Algorithm Design

Algorithm Design

Jon Kleinberg、Éva Tardos / Addison-Wesley / 2005-3-26 / USD 144.20

Algorithm Design introduces algorithms by looking at the real-world problems that motivate them. The book teaches students a range of design and analysis techniques for problems that arise in compu......一起来看看 《Algorithm Design》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器