描述
方法closerange()關閉從 fd_low(包含)到 fd_high(不包含)的所有文件描述符,忽略錯誤。該方法在 Python 2.6 版本中引入。
用法
以下是語法closerange()方法 -
os.closerange(fd_low, fd_high)
參數
fd_low- 這是要關閉的最低文件描述符。
fd_high- 這是要關閉的最高文件描述符。
此函數相當於 -
for fd in xrange(fd_low, fd_high):
try:
os.close(fd)
except OSError:
pass
返回值
此方法不返回任何值。
示例
下麵的例子展示了 closerange() 方法的用法。
#!/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 a single opened file
os.closerange( fd, fd)
print ("Closed all the files successfully!!")
結果
這將創建給定的文件foo.txt然後在該文件中寫入給定的內容。這將產生以下結果 -
Closed all the files successfully!!
相關用法
- Python 3 os.close()用法及代碼示例
- Python 3 os.chflags()用法及代碼示例
- Python 3 os.chmod()用法及代碼示例
- Python 3 os.chroot()用法及代碼示例
- Python 3 os.chdir()用法及代碼示例
- Python 3 os.chown()用法及代碼示例
- Python 3 os.fstatvfs()用法及代碼示例
- Python 3 os.minor()用法及代碼示例
- Python 3 os.unlink()用法及代碼示例
- Python 3 os.major()用法及代碼示例
- Python 3 os.rmdir()用法及代碼示例
- Python 3 os.fdopen()用法及代碼示例
- Python 3 os.fdatasync()用法及代碼示例
- Python 3 os.isatty()用法及代碼示例
- Python 3 os.rename()用法及代碼示例
- Python 3 os.walk()用法及代碼示例
- Python 3 os.renames()用法及代碼示例
- Python 3 os.makedirs()用法及代碼示例
- Python 3 os.utime()用法及代碼示例
- Python 3 os.tcgetpgrp()用法及代碼示例
注:本文由純淨天空篩選整理自 Python 3 - os.closerange() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。