2014-10-29 76 views
0

我是該領域的新手,我想創建一個有用且快速處理的程序,並運行此腳本,我收到此錯誤,並且可以沒有得到這個錯誤。TypeError:無法連接'str'和'dict'對象

# -*- coding: cp1252 -*- 
import ftplib, os 
from ftplib import FTP 

def getFTPConfig(FTPLib): 
    if os.path.exists("./incluide/Config.json"): 
     with open("./incluide/Config.json") as ConfigFTP: 
      FTPConnect = ConfigFTP.read() 
    else: 
     os.system("title Config FTPConnect") 
     Host = raw_input("Enter the host: ") 
     Login = raw_input("Enter the login: ") 
     Pass = raw_input("Enter de password: ") 
     file = open("./incluide/Config.json", "wb") 
     file.write('''{"Host": "'''+Host+'''", 
"Login: "'''+Login+'''", 
"Pass: "'''+Pass+'''"}''') 
     file.close() 
     print "Successfully created" 
     os.system("FTP Session") 
    try: 
     FTPs = json.loads(FTPConnect) 
     return FTPs[FTPLib] 
    except: 
     return {} 

    with open("./incluide/Config.json") as ConfigFTP: 
     FTPConnect = ConfigFTP.read() 
     print('Conectando ao Servidor FTP... Espere um momento...') 

     FTPConnect = FTP(Host, Login, Pass) 

     File = "Session.py" #Arquivo a ser enviado 

     file = open('%s' %(File),'rb') 
     print('Conectado.') 

     print('Enviando arquivo... Espere um momento...') 

     session.storbinary('STOR %s' %(File), file) 

     print('Arquivo enviado!') 

     file.close() 
     session.quit() 

if __name__ == "__main__": 
    Title = "ERROR CONNECTING TO FTP" 
    os.system('cls');os.system('title '+Title) 
    FTPError = """ 
       """+Title+""" 
       Check the connection: 
       Host: """+getFTPConfig('Host')+""" 
       Login: """+getFTPConfig('Login')+""" 
       Pass: """+getFTPConfig('Pass')+"""""" 
    print (FTPError).center(80) 
    time.sleep(5) 

它發出此錯誤

Traceback (most recent call last): 
    File "C:\Users\Desktop\FTP\Session.py", line 59, in <module> 
    Pass: """+getFTPConfig('Pass')+"""""" 
TypeError: cannot concatenate 'str' and 'dict' objects 
+0

很明顯,getFTPConfig('Pass')返回字典在這裏...你嘗試調試它嗎? – 4rlekin 2014-10-29 13:58:56

回答

5

錯誤消息應該清楚,你不能使用+操作總結字符串和字典,也許你想在字典轉換爲字符串。如果getFTPConfig( '合格')返回一個字典,然後

str(getFTPConfig('Pass'))+"whatever" 
3
web_response = {2L: 67.0, 3L: 13.67, 4L: 10.25, 5L: 11.8, 6L: 11.83} 

我命名 「web_response」

一本字典,對於 字典用繩子的級聯我用 逗號 「」
print "web_response=", web_response 

輸出:

web_response= {2L: 67.0, 3L: 13.67, 4L: 10.25, 5L: 11.8, 6L: 11.83} 
相關問題