Python Forum
What is the sys.stdin.isatty() command?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the sys.stdin.isatty() command?
#1
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))
Reply
#2
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.
Reply
#3
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  When is stdin not connected to a tty, but can still be used interactively? stevendaprano 1 1,018 Sep-24-2022, 05:06 PM
Last Post: Gribouillis
  STDIN not working H84Gabor 3 3,594 Sep-06-2021, 08:10 AM
Last Post: H84Gabor
  How to read a file using stdin? pyth0nus3r 1 2,847 Aug-24-2019, 01:14 AM
Last Post: Larz60+
  getting error "exec_proc.stdin.write(b'yes\n') IOError: [Errno 32] Broken pipe" Birju_Barot 0 2,947 Jul-23-2019, 11:50 AM
Last Post: Birju_Barot
  is it safe to close stdin Skaperen 1 2,683 Apr-04-2019, 06:57 AM
Last Post: Gribouillis
  Need help with reading input from stdin into array list Annie123 2 5,080 Mar-24-2019, 01:19 PM
Last Post: Annie123
  detect if sys.stdin is ... Skaperen 4 8,113 May-30-2018, 11:49 PM
Last Post: Skaperen
  storing lines from stdin in a list bghosh 2 2,999 May-02-2018, 01:12 PM
Last Post: gruntfutuk
  How can I use GNU readline when the value of sys.stdin has been changed? thePhysicist8 6 7,114 Dec-30-2016, 10:09 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020