How can I disable the webbrowser message in python?

后端 未结 2 681
时光说笑
时光说笑 2021-02-04 08:49

In my python program, when I send the user to create a gmail account by use of the webbrowser module, python displays:

\"Please enter your Gmail username: Created new wi

2条回答
  •  甜味超标
    2021-02-04 09:15

    As S.Lott hints in a comment, you should probably do the raw_input first; however, that, per se, doesn't suppress the message from webbrowser, as you ask -- it just postpones it.

    To actually suppress the message, you can temporarily redirect standard-output or standard-error -- whichever of the two your chosen browser uses to emit that message. It's probably no use to redirect them at Python level (via sys.stdout or sys.stderr), since your browser is going to be doing its output directly; rather, you can do it at the operating-system level, e.g., for standard output:

    import os
    gmail_user = raw_input('Please enter your Gmail username: ')
    savout = os.dup(1)
    os.close(1)
    os.open(os.devnull, os.O_RDWR)
    try:
       webbrowser.open(whatever)
    finally:
       os.dup2(savout, 1)
    

    (for standard error instead of standard output, use 2 instead of 1). This is pretty low-level programming, but since the webbrowser module does not give you "hooks" to control the way in which the browser gets opened, it's pretty much the only choice to (more or less) ensure suppression of that message.

提交回复
热议问题