Easy Tutorial
❮ Python Basic Syntax Python Func Ord ❯

Python3 os.closerange() Method

Python3 OS File/Directory Methods


Overview

The os.closerange() method is used to close all file descriptors from fd_low (inclusive) to fd_high (exclusive), ignoring any errors.

Syntax

The syntax for the closerange() method is as follows:

os.closerange(fd_low, fd_high);

Parameters

This method is similar to:

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

Return Value

This method does not return any value.

Example

The following example demonstrates the use of the closerange() method:

#!/usr/bin/python3

import os, sys

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

# Write a string
os.write(fd, "This is a test")

# Close the file
os.closerange(fd, fd)

print("Successfully closed the file!!")

Executing the above program will output:

Successfully closed the file!!

Python3 OS File/Directory Methods

❮ Python Basic Syntax Python Func Ord ❯