Python Forum
impossible to exit from the file
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
impossible to exit from the file
#1
Hi all ! This file works well, unless I try to exit. Look please at lines 17,18, 19. I am on Linux. Python 3.7.
import tty, sys, termios

class ReadChar():
    def __enter__(self):
        self.fd = sys.stdin.fileno()
        self.old_settings = termios.tcgetattr(self.fd)
        tty.setraw(sys.stdin.fileno())
        return sys.stdin.read(1)
    def __exit__(self, type, value, traceback):
        termios.tcsetattr(self.fd, termios.TCSADRAIN, self.old_settings)

def test():
    while True:
        with ReadChar() as rc:
            char = rc
        #if ord(char) == 3:
		#	sys.exit()   
        #elif ord(char) <= 32:
        if ord(char) <= 32:
            print("You entered character with ordinal {}."\
                        .format(ord(char)))
        else:
            print("You entered character '{}'."\
                        .format(char))
        #if char in "^C^D":
         #   sys.exit()

if __name__ == "__main__":
    test()
If I uncomment lines 17, 18, I have the following error:
Error:
sylvain@sylvain-HP-Laptop-15-bw0xx:~$ python get2.py File "get2.py", line 18 sys.exit() ^
Reply


Messages In This Thread
impossible to exit from the file - by sylas - Sep-11-2018, 07:13 AM
RE: impossible to exit from the file - by sylas - Sep-11-2018, 09:14 AM
RE: impossible to exit from the file - by volcano63 - Sep-12-2018, 08:18 PM
RE: impossible to exit from the file - by sylas - Sep-13-2018, 06:38 AM
RE: impossible to exit from the file - by volcano63 - Sep-13-2018, 10:05 AM
RE: impossible to exit from the file - by buran - Sep-13-2018, 06:46 AM
RE: impossible to exit from the file - by DeaD_EyE - Sep-13-2018, 10:39 AM
RE: impossible to exit from the file - by volcano63 - Sep-13-2018, 12:06 PM
RE: impossible to exit from the file - by sylas - Sep-13-2018, 02:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python difference between sys.exit and exit() mg24 1 1,928 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  Impossible to cleanly uninstall Python 3.8.9 Ftaniere 2 1,774 Sep-01-2022, 10:38 AM
Last Post: snippsat
  Failing reading a file and cannot exit it... tester_V 8 1,873 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Even if Atom is on the whisker menu, impossible to run it sylas 9 4,420 Jun-21-2018, 07:32 AM
Last Post: sylas

Forum Jump:

User Panel Messages

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