Select to view content in your preferred language

Multiple workspaces

5231
1
01-02-2015 04:13 PM
AmyKlug
Regular Contributor


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!")
0 Kudos
1 Reply
XanderBakker
Esri Esteemed Contributor

You only need to set the workspace when you use the listfiles. For the rest just combine the in_ws and out_ws to obtain a reference to the source and destination, like this:

import arcpy, os

def main():
    import traceback
    in_ws = r"C:\Forum\CopyFiles\InFolder"
    out_ws = r"C:\Forum\CopyFiles\OutFolder"

    cpfiles = find_all_files(in_ws)
    try:
        for c in cpfiles:
            destination = os.path.join(out_ws, c)
            if not arcpy.Exists(destination):
                arcpy.AddMessage("Copying {0} to {1}".format(c, out_ws))
                arcpy.Copy_management(os.path.join(in_ws, c), destination)
        del c
    except:
        traceback.print_exc()
        print("Cannot copy {0}, copy it over manually and run script again".format(c))
    finally:
        print("Finished!")

def find_all_files(workspace):
    arcpy.env.workspace = workspace
    files = arcpy.ListFiles()
    return files

if __name__ == '__main__':
    main()
0 Kudos