1

I'm working on Arcgis 10.5 and I am trying to achieve this:

On my script, I want to make folders and subfolder, and also I want to copy one specific folder into several subfolders at the same time. My script is working well to make the folders and subfolder.

However, I get the next error when the script tried to copy the folder I want :

TypeError: cannot concatenate 'str' and 'Result' objects.

This my script:

I can't see where is the mistake so can somebody can give me a hand?

import arcpy
import os
import shutil
#Workspaces
arcpy.env.workspace = r'F:\FOLDER'
Folder_to_copy = r'F:\FOLDER\FOLDER1\FOLDER2\BASE_CHIH_MZA_UTM13_2018' #<--I want to copy this folder into the subfolders Sub1,Sub2,Sub3,Sub4

#Folder to make
Sub1_Folders = ['Sub1FolderA','Sub1FolderB','Sub1FolderC']
Sub2_Folders = ['Sub1','Sub2','Sub3','Sub4'] #<--The Especific folders where I want to copy my folder

#Result
Out_putfolder = r'F:\FOLDER\OutPutFolder'

#Field dbf to make subfolder
Tabla_dbf = r'F:\FOLDER\BRIGADISTA.dbf'
Field1 = 'BRIGADISTA'

Brigadistas = [row[0] for row in arcpy.da.SearchCursor(Tabla_dbf,Field1)]
Unicos_Briga = set(Brigadistas)

#Make folder and subfolders
for f1  in Sub1_Folders:
    Sub1folder = arcpy.CreateFolder_management(Out_putfolder,f1)
    for f2 in Sub2_Folders:
        Sub2folders = arcpy.CreateFolder_management(Sub1folder,os.path.join(f2))
        for tb in Unicos_Briga:
            tabladbf = arcpy.CreateFolder_management(Sub2folders,os.path.join(tb))
            Copyfolder = shutil.copytree(Folder_to_copy,os.path.join(tabladbf))

print 'Done'

This is the error show up

Runtime error 
Traceback (most recent call last):
  File "<string>", line 29, in <module>
  File "C:\Python27\ArcGIS10.5\Lib\ntpath.py", line 90, in join
    return result_drive + result_path
TypeError: cannot concatenate 'str' and 'Result' objects
  • You need to understand that all geoprocessing tools return a result object, one of its properties might be the name of a resulting featureclass or a value. So you are trying to concatenate an object with a string, which makes no sense hence the error. A typical error (which you are doing) is assuming the result of a tool is a dataset, no it is a result object which you can query for the output dataset: read the help file on result objects. – Hornbydd Jun 27 '18 at 22:51
2

Based on the error, you need to use .getOutput off the result of your CreateFolder operation. This will return the path as a string so you can use it with concatenate or os.path.join

subfolder = arcpy.CreateFolder_management("c:/temp", "foo")
subfolder.getOutput(0)
>> 'c:/temp\\foo'
type(subfolder.getOutput(0))
>> <class 'str'>

Based on the code in your question, here is where I'd put getOutput(0). I'd attach it directly to the CreateFolder call because you're assinging the output of that to a variable. So order of operations for your sub1folder variable means the tool runs then the result object is got and assigned to the variable.

for f1  in Sub1_Folders:
    Sub1folder = arcpy.CreateFolder_management(Out_putfolder,f1).getOutput(0)
    for f2 in Sub2_Folders:
        Sub2folders = arcpy.CreateFolder_management(Sub1folder,os.path.join(f2)).getOutput(0)
        for tb in Unicos_Briga:
            tabladbf = arcpy.CreateFolder_management(Sub2folders,os.path.join(tb))
            Copyfolder = shutil.copytree(Folder_to_copy,os.path.join(tabladbf))
  • Thank you for answer but I don't understand your idea. Where exactly I need to type .getOutput on my script – Victor Quiroz Barrientos Jun 27 '18 at 22:21
  • I used the code from your question with the piece you need. – KHibma Jun 28 '18 at 1:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.