The Wayback Machine - https://web.archive.org/web/20240213201439/https://www.geeksforgeeks.org/python-os-isatty-method/
Open In App
Related Articles

Python | os.isatty() method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

os.isatty() method in Python is used to check whether the specified file descriptor is open and connected to a tty(-like) device or not. “tty” originally meant “teletype” and tty(-like) device is any device that acts like a teletype, i.e a terminal.

A file descriptor is small integer value that corresponds to a file or other input/output resource, such as a pipe or network socket. It is an abstract indicator of a resource and act as handle to perform various lower level I/O operations like read, write, send etc.

Syntax: os.isatty(fd)

Parameter:
fd: A file descriptor, whose is to be checked.

Return Type: This method returns a Boolean value of class bool. Returns True if the specified file descriptor is open and connected to a tty(-like) device otherwise, returns False.

Code: Use of os.isatty() method to check if the given file descriptor is open and connected to tty(-like) device or not




# Python program to explain os.isatty() method  
  
# importing os module 
import os
  
  
# Create a pipe using os.pipe() method
# It will return a pair of 
# file descriptors (r, w) usable for
# reading and writing, respectively.
r, w = os.pipe()
  
  
# Check if file descriptor r
# is open and connected
# to a tty(-like) device
# using os.isatty() method
print("Connected to a terminal:", os.isatty(r))
  
  
  
# Open a new pseudo-terminal pair
# using os.openpty() method
# It will return master and slave 
# file descriptor for 
# pty ( pseudo terminal device) and
# tty ( native terminal device) respectively
master, slave = os.openpty()
  
# Check if file descriptor master
# is open and connected
# to a tty(-like) device 
# using os.isatty() method
print("Connected to a terminal:", os.isatty(master))
  


Output:

Connected to a terminal: False
Connected to a terminal: True

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 25 Jun, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials