AnsweredAssumed Answered

Multiple workspaces

Question asked by aimong on Jan 2, 2015
Latest reply on Jan 3, 2015 by xander_bakker


I am trying to list data from one workspace, check if it exists in another, and if not, copy it to the new database (because in the middle of copying it can error out on a file and I don't want to wait for it to overwrite everything I have already copied over). I have tried many different ways but whenever I define 2 workspaces in the same code it errors that the file already exists (ignores overwriteOutput = true) or says the file can't be copied because it is not supported/does not exist. Will copy fine when 1 workspace is defined but I have to wait for it to copy everything all over again. Hope this makes sense.

 

works:

 

 

import arcpy, os, traceback
arcpy.env.overwriteOutput = True
arcpy.env.workspace = in_ws
cpfiles = arcpy.ListFiles()
try:
    for c in cpfiles:
        in_path, in_name = os.path.split(c)
        arcpy.AddMessage("Copying " + in_name + " to " + out_path + in_name)
        arcpy.Copy_management(c, out_path + in_name) 
    del c
except:
    traceback.print_exc()
    raw_input("Cannot copy" + c + ", copy it over manually and run script again")
finally:
    raw_input("Finished!")

 

does not work:

 

 

import arcpy, os, traceback
arcpy.env.overwriteOutput = True

def find_all_files(workspace):
    prev_workspace = arcpy.env.workspace
    arcpy.env.workspace = workspace
    files = arcpy.ListFiles()
    arcpy.env.workspace = prev_workspace
    return files
cpfiles = find_all_files(in_ws)
exfiles = find_all_files(out_ws)
try:
    for c in cpfiles:
        in_path, in_name = os.path.split(c)
        if not c in exfiles:
            #arcpy.AddMessage(out_path + in_name) #get correct message, lists file not yet copied over
            arcpy.AddMessage("Copying " + in_name + " to " + out_path + in_name)
            arcpy.Copy_management(c, out_path + in_name) # error does not exist/is not supported
    del c 
except:
    traceback.print_exc()
    raw_input("Cannot copy " + c + ", copy it over manually and run script again")
finally:
    raw_input("Finished!")

Outcomes