Python os.set_inheritable() Method

Python os.set_inheritable() Method

  1. Syntax
  2. Parameters
  3. Returns
  4. Examples Code: Set the inheritable Flag for a File

The Python programming language offers an os module to interact with the operating system and perform various low-level operations such as reading files, spawning new processes, file management, environment operations, etc.

Whenever a file is opened, a file descriptor is associated with that file. A file descriptor is essentially an integer value uniquely identifying an open file on a system.

A file descriptor has an inheritable flag that indicates if any child process can inherit that file descriptor. By default, all the file descriptors are set as non-inheritable.

On UNIX and Windows systems, non-inheritable file descriptors are closed inside child processes except for inheritable file descriptors.

The os module has a set_inheritable() method that we can use to set the inheritable flag of a valid file descriptor to a Boolean value.


os.set_inheritable(fd, inheritable)


Type Explanation
fd Integer It is a file descriptor associated with a valid resource.
inheritable Boolean A boolean value for the inheritable attribute.


The set_inheritable() method does not return anything. Instead, it sets a file descriptor’s inheritable flag to a boolean value.

Examples Code: Set the inheritable Flag for a File

Code Snippet:

import os

path = os.getcwd() + "/file.txt"
fd =, os.O_RDWR | os.O_CREAT)
print("Inheritable Flag Status:", os.get_inheritable(fd))
os.set_inheritable(fd, True)
print("Inheritable Flag Status:", os.get_inheritable(fd))


Inheritable Flag Status: False
Inheritable Flag Status: True

The Python code above first defines a path to a file. It can be a file system path to any file, but the above code considers a file file.txt in the current working directory.

The os.getcwd() method returns a string representation for the current working directory. Next, we open the file using the method.

Note that if the file is missing, it is created and ultimately opened in read and write mode. Currently, the file descriptor is inheritable (refer to the first line of the output)

Lastly, we set the inheritable flag to True. The second line in the output depicts the same.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

LinkedIn GitHub

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method