os.closerange() Method



Description

The method closerange() closes all file descriptors from fd_low (inclusive) to fd_high (exclusive), ignoring errors.This method is introduced in Python version 2.6.

Syntax

Following is the syntax for closerange() method −

os.closerange(fd_low, fd_high)

Parameters

  • fd_low − This is the Lowest file descriptor to be closed.

  • fd_high − This is the Highest file descriptor to be closed.

This function is equivalent to −

for fd in xrange(fd_low, fd_high):
   try:
      os.close(fd)
   except OSError:
      pass

Return Value

This method does not return any value.

Example

The following example shows the usage of closerange() method.

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!!")

This would create given file foo.txt and then write given content in that file. This will produce the following result −

Closed all the files successfully!
python_os_file_directory_methods.htm
Advertisements