无法将对象隐式转换为str

时间:2016-07-18 23:42:55

标签: python python-3.x

我是python的新手。我有以下代码,其中有一个类有三个方法问题在winCMD = 'NET USE '+ host + ' /User:' + self.user + ' ' + self.password行,它总是抱怨self.userself.password因为无法将对象转换为int。关于我做错什么的任何想法?

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 个答案:

答案 0 :(得分:1)

所有您的功能中,您实际上并未将self 声明为第一个参数,因此 host得到了分配到了对象实例。当你尝试将它添加到另一个带有“”.. + host + ..“的字符串时,你会收到你收到的错误。

将所有函数声明更改为使用self 作为第一个参数,问题应该留下:

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

为:

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