Python - 警告:意外错误:<class'indexerror'=“”>

时间:2018-02-13 12:50:05

标签: python error-handling amazon-redshift

我正在尝试使用一些代码从名为“.logon”的文件中读取用户名和密码,并建立与redshift的连接。但我收到错误消息“Python - 警告:意外错误:”。

这是我的代码:

def redshift_connect():  
    try:  
        l_user = getpass.getuser()  
        home = expanduser("~")  
        f = open(home + '/.logon')  
        logline = f.readline()  
        usr = logline.split(',')[0].split('/')[1].strip()  
        pwd = logline.split(',')[1].rstrip()  


        conn=psycopg2.connect(host='host-name',  
        port= 5439, user= usr, password= pwd)  
        return conn  
    except IOError as e:  
        print ('I/O error ({0}): {1}: .tdlogon missing!!'.format(e.errno, e.strerror))
    except:  
        print ('Warning: Unexpected Error:',sys.exc_info()[0])  
    else:
        raise ConnectionError('Cannot establish connectionw with Redshift.')

1 个答案:

答案 0 :(得分:1)

我猜表达式logline.split(',')有效并返回一个只有一个条目的数组。然后,表达式logline.split(',')[1]会引发您在非特定IndexError子句中捕获的except

相关问题