2014-01-17 7 views
0

Я получил эту ошибку TypeError: cannot concatenate 'str' and 'builtin_function_or_method' objects, когда я попытался сменить каталог на ftp.ТипError: не может concatenate 'str'

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 

Что там происходит? и почему эта ошибка?

ответ

3

Ошибка здесь:

FILE_TO = ftp_obj.change_directory(dir) 

вы не объявили dir вар, встроенная функция dir передается вместо этого.

+0

Woohoo, exact. – aIKid

+0

Shit, я изменил код из 'ftp_obj.make_dir ('/ test')' в целях тестирования и забыл инициализировать 'dir' снова. Благодарю. – ajkumar25

Смежные вопросы