TypeError:无法连接“ str”

阿金佐尔

TypeError: cannot concatenate 'str' and 'builtin_function_or_method' objects当我尝试更改ftp中的目录时出现此错误

ftp.py

from ftplib import FTP

ftp = FTP()

class FTPClient():

    connection_id = None
    login_ok = False
    message_array = []

    def __init__(self):
        pass

    def log_message(self, message, clear=True):
        if clear:
            self.message_array = message

    def get_message(self):
        return self.message_array

    def connect(self, server, ftp_user, ftp_password, is_passive = False):
        self.connection_id = ftp.connect(server)
        login_result = ftp.login(user=ftp_user, passwd=ftp_password)
        ftp.set_pasv(is_passive)

        if not self.connection_id or not login_result:
            self.log_message("FTP Connection has Failed")
            self.log_message("Attempted to connect to {0} for {1}".format(server, ftp_user))
            return False
        else:
            self.log_message("Connected to {0} for {1}".format(server, ftp_user))
            self.login_ok = True
            return True

    def make_dir(self, directory):
        if ftp.mkd(directory):
            self.log_message("Directory {0} created successfully".format(directory))
            return True
        else:
            self.log_message("Failed creating directory")
            return False

    def change_directory(self, directory):
        if ftp.cwd(directory):
            self.log_message("Current Directory is now {0}".format(ftp.pwd))
        else:
            self.log_message("Can't change Directory")

    def upload_file(self, file_from, file_to):
        if file_from.endswith(('.csv', '.txt')):
            with open(file_from, 'r') as f:
                self.connection_id.storelines("Uploaded from {0} to {1}".format(file_from, file_to), f)
        else:
            with open(file_from, 'rb') as f:
                self.connection_id.storebinary('Uploaded from {0} to {1}'.format(file_from, file_to), f)


ftp_obj = FTPClient()

FTP_HOST = "yyy"
FTP_USER = "xxx"
FTP_PASS = "zzz"

ftp_obj.connect(FTP_HOST, FTP_USER, FTP_PASS)
print(ftp_obj.get_message())
FILE_FROM = "config.txt"
FILE_TO = ftp_obj.change_directory(dir)
ftp_obj.upload_file(FILE_FROM, FILE_TO)
print(ftp_obj.get_message())

完整回溯:

Connected to yyy for xxx
Traceback (most recent call last):
  File "C:/Users/Ajay/PycharmProjects/database/test.py", line 67, in <module>
    FILE_TO = ftp_obj.change_directory(dir)
  File "C:/Users/Ajay/PycharmProjects/database/test.py", line 44, in change_directory
    if ftp.cwd(directory):
  File "C:\Python27\lib\ftplib.py", line 552, in cwd
    cmd = 'CWD ' + dirname
TypeError: cannot concatenate 'str' and 'builtin_function_or_method' objects

那里发生了什么事?以及为什么这个错误?

恩普

错误在这里:

FILE_TO = ftp_obj.change_directory(dir)

您尚未声明dirvar,dir而是传递了内置函数

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError:无法将字节连接到str

TypeError:无法连接“ str”和“ UUID”对象

TypeError:无法连接“ str”和“ Redditor”对象

Python:TypeError:无法连接“str”和“int”

TypeError:无法连接“ str”和“ float”对象:pandas

Python:TypeError无法连接'str和'list'对象

Python 3.6 urllib TypeError:无法将字节连接到str

Python Selenium TypeError:无法连接“ str”和“ WebElement”对象

TypeError:无法连接“ str”和“ pygame.Surface”对象

Python:TypeError:无法连接“ str”和“ int”对象

python3-TypeError:无法将字节连接到str

re.search()TypeError:无法连接“ str”和“ NoneType”对象

TypeError:无法连接'str'和'function'对象的python文件

lines + = line / TypeError:无法将str连接为字节

Tkinter Label,TypeError:无法连接“ str”和“ instance”对象

abaqus脚本:TypeError:无法连接“ str”和“ Set”对象

Python TypeError:无法连接“ str”和“ list”对象

TypeError:无法连接电子邮件中的“ str”和“ list”对象

TypeError:无法将字节连接到str时如何转换为字节

TypeError:无法连接类型为“ <class'str'>”的对象;仅Series和DataFrame objs有效

Python TypeError:尝试打印时无法连接“ str”和“ int”对象

TypeError:无法连接'str'和'NoneType'对象python bs4

TypeError:无法使用MySQL连接Flask中的'str'和'float'对象

Python TypeError:只能将 str(而不是“字节”)连接到 str

TypeError:只能将str(而不是“ float”)连接到str

TypeError:只能将str(不是“ int”)连接到str SUM

错误:TypeError:只能将 str(不是“NoneType”)连接到 str

Scrapy - TypeError:只能将 str (不是“列表”)连接到 str

无法将字节连接到str