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


上一篇: Python3文件方法 下一篇: Python3 os文件目录的方法