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檔目錄的方法
