Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python2.7中捕获警告而不停止部分程序_Python_Python 2.7_Warnings_Try Except - Fatal编程技术网

在Python2.7中捕获警告而不停止部分程序

在Python2.7中捕获警告而不停止部分程序,python,python-2.7,warnings,try-except,Python,Python 2.7,Warnings,Try Except,我目前正在开发一个Python程序,其中包括使用用户名和密码登录。我正在使用getpass模块来实现这一点。我的问题是,在getpass无法控制echo的情况下,它会在继续执行程序之前将以下内容喷入终端 Warning (from warnings module): File "C:\Python27\lib\getpass.py", line 92 return fallback_getpass(prompt, stream) GetPassWarning: Can not con

我目前正在开发一个Python程序,其中包括使用用户名和密码登录。我正在使用
getpass
模块来实现这一点。我的问题是,在getpass无法控制echo的情况下,它会在继续执行程序之前将以下内容喷入终端

Warning (from warnings module):
  File "C:\Python27\lib\getpass.py", line 92
    return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
我想做的是捕捉警告并打印我自己的自定义消息。我能想到的唯一代码是以下代码,它阻止显示回溯,但根本不打印自定义消息

import getpass

try:
    getpass.getpass("Password: ")
except getpass.GetPassWarning:
    print "Oh no!"
输出:

Warning: Password input may be echoed.
Password: 
Custom message.
Password: 

我想用我自己的消息替换文本,
Warning:Password输入可能会被回显。
,理想情况下。

getpass
使用python内置模块
warnings
显示此警告消息。您可以使用各种方法(/)筛选/忽略它们

您可以这样捕捉警告:

import getpass
import warnings

# the context manager resets the original
# filterwarnings after it has exited
with warnings.catch_warnings():
    # this will raise warnings of type (or inherited of type)
    # 'getpass.GetPassWarning' in the module 'getpass'
    warnings.filterwarnings(
        'error',
        category=getpass.GetPassWarning,
        module='getpass'
    )
    try:
        password = getpass.getpass('Password: ')
    except getpass.GetPassWarning:
        print 'Cannot get password on insecure platform'

@PM2Ring建议的方法似乎是迄今为止我找到的最好的解决方案。为了其他路过的人,为了回答这个问题,我将在这篇文章中总结所有内容

以下方法覆盖了
getpass
模块中的
fallback\u getpass
函数,允许用户准确控制在需要回退时发生的情况。虽然有点麻烦,但它完成了任务

import getpass

def custom_fallback(prompt="Password: ",stream=None): #Clone of parameters from getpass.fallback_getpass
    print "Custom message." #Custom message goes here
    return getpass._raw_input(prompt) #Use getpass' custom raw_input function for security

getpass.fallback_getpass = custom_fallback #Replace the getpass.fallback_getpass function with our equivalent

password = getpass.getpass("Password: ") #Prompt for password
输出:

Warning: Password input may be echoed.
Password: 
Custom message.
Password: 

此问题:讨论如何抑制警告,其中包含指向
警告
模块文档的链接。FWIW,这是的Python源代码。@PM2Ring是否允许我在提示之前打印自定义消息?我不这么认为(但我从未使用过
警告
模块)。我想你可以用你自己的函数替换导入的
getpass.fallback\u getpass
,尽管这种对标准模块函数的修补实际上只能作为最后的手段,IMHO。不幸的是,这两种解决方案似乎与我的尝试具有相同的效果。回溯被阻止,但不显示自定义消息:/首先,我实际上无法复制
GetPassWarning
——您是在哪个平台上获得它的?我在Windows 10上使用的是Python 2.7.11。在IDLE中最容易复制。我已经更新了代码-这对我在Windows 10/py 2.7/IDLE和OS X上使用py 2.7都有效。现在,文本打印成功,但回溯和
警告:…
文本仍然保留着:/