2014-01-17 2 views
0

ftp에서 디렉토리를 변경하려고 시도했을 때 TypeError: cannot concatenate 'str' and 'builtin_function_or_method' objects이 오류가 발생했습니다.TypeError : '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 VAR 대신 전달 내장 기능 dir를 선언하지 않았습니다.

+0

우후, 정확합니다. – aIKid

+0

젠장,'ftp_obj.make_dir ('/ test')'코드를 테스트 목적으로 변경하고'dir'을 다시 초기화하는 것을 잊어 버렸습니다. 감사. – ajkumar25

관련 문제