nikhil nikhil - 4 months ago 10
Python Question

Cannot convert object to str implicitly python

I am very new to python. I have the following code that has a class with three methods problem is at winCMD = 'NET USE '+ host + ' /User:' + self.user + ' ' + self.password line it always complains for self.user and self.password as cannot convert object to int. Please help what am I doing wrong.

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")

Jim Jim
Answer

In all your functions you're not actually declaring self as the first parameter and as a result host get's assigned to the object instance. When you try to add that to another string with `".. + host + .." you'll get the error you're receiving.

Change all your function declarations to use self as the first parameter and the issue should leave:

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

to:

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