Python TypeError:无法连接';str';
当我试图更改ftp中的目录时,我遇到了这个错误Python TypeError:无法连接';str';,python,ftp,Python,Ftp,当我试图更改ftp中的目录时,我遇到了这个错误TypeError:无法连接'str'和'builtin\u function\u或'u method'对象 ftp.py from ftplib import FTP ftp = FTP() class FTPClient(): connection_id = None login_ok = False message_array = [] def __init__(self): pass
TypeError:无法连接'str'和'builtin\u function\u或'u method'对象
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
已连接到xxx的yyy
回溯(最近一次呼叫最后一次):
文件“C:/Users/Ajay/PycharmProjects/database/test.py”,第67行,在
FILE_TO=ftp_obj.change_目录(dir)
文件“C:/Users/Ajay/PycharmProjects/database/test.py”,第44行,在change_目录中
如果是ftp.cwd(目录):
cwd中第552行的文件“C:\Python27\lib\ftplib.py”
cmd='CWD'+dirname
TypeError:无法连接“str”和“内置函数”或“方法”对象
那里发生了什么事?为什么会出现这种错误?错误在这里:
FILE_TO = ftp_obj.change_directory(dir)
您没有声明dir
var,而是传递了内置函数dir
。错误如下:
FILE_TO = ftp_obj.change_directory(dir)
您没有声明dir
var,而是传递了内置函数dir
。错误如下:
FILE_TO = ftp_obj.change_directory(dir)
您没有声明dir
var,而是传递了内置函数dir
。错误如下:
FILE_TO = ftp_obj.change_directory(dir)
您没有声明dir
var,而是通过了内置函数dir
。Shit,我将代码从ftp\u obj.make\u dir('/test')
更改为测试目的,忘了再次初始化dir
。谢谢。妈的,我把代码从ftp\u obj.make\u dir('/test')
改为测试目的,又忘了初始化dir
。谢谢。妈的,我把代码从ftp\u obj.make\u dir('/test')
改为测试目的,又忘了初始化dir
。谢谢。妈的,我把代码从ftp\u obj.make\u dir('/test')
改为测试目的,又忘了初始化dir
。谢谢