5

I'm catching ctrl-c with a signal handler and was wondering if there is a nicer way to hide the ^C that is printed to terminal than go os.system("\b") or similar. I only really care about Unix.

    def read(self, time_s):

        setup = ['Sensor', 'sensors', '\x00']

        # Closure for signal handler to allow access to self
        def sigintHandler(*args):
            self.close()
            sys.stdout.write('\b\b\r')  # Current solution
            sys.stdout.flush()
            sys.exit(0)

        signal.signal(signal.SIGINT,sigintHandler)

Edit

My current solution is above

chris
  • 4,600
  • 4
  • 32
  • 64
  • Take a look at http://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python – MeetTitan Dec 02 '14 at 23:16
  • I cant find anything there that stops the print? Or is that the answer which overwrites __exit__ and stuff? – chris Dec 02 '14 at 23:19
  • That is for overriding what ctrl-c does. Are you looking to "silence" ctrl-c and keep it's functionality? – MeetTitan Dec 02 '14 at 23:21
  • 2
    yeah basically still function the same but stop it printing ^C – chris Dec 02 '14 at 23:22
  • chris is looking alternative to hide ```^C``` text printed when ```KeyboardInterrupt``` occurs – Gtx Mar 06 '16 at 22:41
  • The terminal prints it way before it reaches Python. I don't think there is a more viable Python solution to this. – tripleee Sep 27 '16 at 07:20

1 Answers1

1

Using print('\b\b\r') will clear the annoying ^C from the output.

user4157124
  • 2,533
  • 12
  • 24
  • 39
dbrennan
  • 11
  • 1