2016-07-18 8 views
0

Я очень новичок в python. У меня есть следующий код, который имеет класс с тремя методами, проблема находится на winCMD = 'NET USE '+ host + ' /User:' + self.user + ' ' + self.password строке, он всегда жалуется на self.user и self.password, так как не может преобразовать объект в int. Любые идеи о том, что я делаю неправильно?Невозможно преобразовать объект в str неявно python

import subprocess 
#from shutil import copyfile 
import shutil 
import os 

class user_credentials: 
    def __init__(self, user, password): 
     self.user=user 
     self.password=password 

    def remoteCopyFile(host, self, source, destination): 

     winCMD = 'NET USE '+ host + ' /User:' + self.user + ' ' + self.password 
     subprocess.call(winCMD, shell=True) 
     getFileName=os.path.basename(source) 
     tempDestination=r"{0}\{1}".format(destination, getFileName) 
     try: 
      if not os.path.exists(tempDestination): 
       shutil.copy(source, destination) 
       print("Copied the Installer File Successfully") 
      else: 
       print("File alreade exists. Delete and recreate") 
     except: 
      e=sys.exc_info()[0] 
      print("Something went wrong: %s "%e) 


    def remoteCopyFolder(host, self, source, destination): 

     winCMD = 'NET USE '+ host + ' /User:' + self.user + ' ' + self.password 
     subprocess.call(winCMD, shell=True) 
     getDirectoryName=os.path.basename(source) 
     newDestination=r"{0}\{1}".format(destination, getDirectoryName) 

     try: 
      if not os.path.exists(newDestination): 
       print("copying files please wait....") 
       shutil.copytree(source, newDestination) 
       print("Copied the entire directory successfully") 
      else: 
       print("That folder already exists. Delete and recreate again") 
     except: 
      e=sys.exc_info()[0] 
      print("Something went wrong: %s "%e) 


    def createFolderOnNetwork(host, self, destination, folderName): 
     winCMD = 'NET USE '+ host + ' /User:' + self.user + ' ' + self.password 
     subprocess.call(winCMD, shell=True) 
     newPath=r"{0}\{1}".format(destination, folderName) 
     if not os.path.exists(newPath): 
      os.makedirs(newPath) 
      print("Created a folder successfully with name "+folderName) 
     else: 
      print("The folder already exists. Delete it and recreate") 


oUser = user_credentials(r'Admin',r'ThePassword') 
host = "10.90.100.193" 
oUser.remoteCopyFile(host,r"\\vm-tfs\Builds\Athena_2.0\Athena_2.0_20160715.6\AltusDataAccessors.dll",r"\\10.90.100.193\Altus_Latest_Build\Binaries\BuildGen_Test") 
oUser.remoteCopyFolder(host,r"\\vm-tfs\Builds\Athena_2.0\Athena_2.0_20160715.6",r"\\10.90.100.193\Altus_Latest_Build\Binaries\BuildGen_Test") 
oUser.createFolderOnNetwork(host,r"\\10.90.100.193\Altus_Latest_Build\Binaries\BuildGen_Test","Test") 

ответ

1

В всех ваших функций вы на самом деле не заявляющие selfв качестве первого параметра и в результате host прибудет Присвоенный экземпляр объекта. Когда вы пытаетесь добавить это в другую строку с помощью «.. .. + host + ..», вы получите полученную вами ошибку.

Изменить все ваши объявления функций для использования selfв качестве первого параметра и этот вопрос должен оставить:

def remoteCopyFile(host, self, source, destination): 

к:

def remoteCopyFile(self, host, source, destination): 
+1

Спасибо Джим работает :) – nikhil

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