Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: cannot concatenate 'str' and 'dict' objects

I am a novice in the field, and I wanted to create a program that was useful and fast handling, and run this script, I get this error, and I could not get this error.

# -*- 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)

It sends this error

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
like image 656
Loukino Avatar asked Oct 29 '14 13:10

Loukino


2 Answers

the error message should be clear, you can't use + operator to sum a string and a dictionary, maybe you want to convert the dict to a string. If getFTPConfig('Pass') is returning a dictionary then

str(getFTPConfig('Pass'))+"whatever"
like image 165
lelloman Avatar answered Oct 24 '22 06:10

lelloman


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

I've a dictionary named "web_response",For concatenation of dictionary with string I used comma ","

print "web_response=", web_response

Output:

web_response= {2L: 67.0, 3L: 13.67, 4L: 10.25, 5L: 11.8, 6L: 11.83}
like image 27
Taimoor Changaiz Avatar answered Oct 24 '22 07:10

Taimoor Changaiz