当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python 3 os.close()用法及代码示例



描述

方法close()关闭与文件描述符 fd 相关联的。

用法

以下是语法close()方法 -

os.close(fd)

参数

fd- 这是文件的文件描述符。

返回值

此方法不返回任何值。

注意− 此函数用于低级 I/O,必须应用于 os.open() 或 pipe() 返回的文件描述符。

示例

下面的例子展示了 close() 方法的用法。

#!/usr/bin/python3
import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string
line = "this is test" 

# string needs to be converted byte object
b = str.encode(line)
os.write(fd, b)

# Close opened file
os.close( fd )

print ("Closed the file successfully!!")

结果

当我们运行上述程序时,它会产生以下结果 -

Closed the file successfully!!

相关用法


注:本文由纯净天空筛选整理自 Python 3 - os.close() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。