Python Forum

Full Version: What is the sys.stdin.isatty() command?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I read this script but I had difficulty understanding the line that says sys.stdin.isatty():

import sys

class Redirection(object):
        def __init__(self, in_obj, out_obj):
                self.input = in_obj
                self.output = out_obj

        def read_line(self):
                res = self.input.readline()
                self.output.write(res)
                return res

if __name__ == '__main__':
        if not sys.stdin.isatty():
                sys.stdin = Redirection(in_obj = sys.stdin, out_obj = sys.stdout)

        a = input('Enter a string: ')
        b = input('Enter another string: ')
        print('Entered strings are: ', repr(a), ' and ', repr(b))
Quote:os.isatty(fd)

Return True if the file descriptor fd is open and connected to a tty(-like) device, else False.
see: https://docs.python.org/3/library/os.htm...#os.isatty

Quote:On Windows, UTF-8 is used for the console device. Non-character devices such as disk files and pipes use the system locale encoding (i.e. the ANSI codepage). Non-console character devices such as NUL (i.e. where isatty() returns True) use the value of the console input and output codepages at startup, respectively for stdin and stdout/stderr. This defaults to the system locale encoding if the process is not initially attached to a console.
Lines 17 to 19, takes input from the user keyboard and concatenates them.
When I omitted lines 1 to 15 from this script, the script worked just like when I had the first 15 lines.
So what is the purpose of lines 1 to 15?