2

My main goal is to export a layer from ArcMap as a shapefile and upload it to AGOL. However, I am stuck at this intersection.

All my scripts work in the python console within ArcMap. However, I cannot run the following as a stand-alone script.

#!python2

import os
import arcpy

# Change Directory
os.chdir(r'C:\ \ \ \ArcGIS\Exports')

# Export Layer Data
arcpy.mapping.MapDocument(r"P:\ \ \*2021.mxd")
analysis = arcpy.CopyFeatures_management('Splice Point', r'C:\ \ \ \ArcGIS\Exports\itemSplice.shp')
print(analysis)

Here is the error I get:
ERROR 000732: Input Features: Dataset Splice Point does not exist or is not supported Failed to execute (CopyFeatures).

It is worth noting that when I run the following code:

import os
import arcpy

mxd = arcpy.mapping.MapDocument(r"P:\ \ \*2021.mxd")
analysis = arcpy.mapping.ListLayers(mxd, None, None)

print(analysis)

I get a list of layers within the .mxd. One of the listed layers (the one I want), matches the layer mentioned in "CopyFeatures_management."

2 Answers 2

5

You were almost there...you need to select the layer from the map document and then use that as the source for the CopyFeatures. e.g.

# Export Layer Data
arcpy.mapping.MapDocument(r"P:\ \ \*2021.mxd")

# this selects the first item from the ListLayers result object that has a name
# matching "Splice Point".
analysis = arcpy.mapping.ListLayers(mxd, "Splice Point", None)[0] 

arcpy.CopyFeatures_management(analysis, r'C:\ \ \ \ArcGIS\Exports\itemSplice.shp')
3

Running code in the python console often has different behaviour than running it as a standalone script. When you're using the console, you're running it in the context of the ArcMap session that you're in.

A couple of comments on your code:

# Export Layer Data
arcpy.mapping.MapDocument(r"P:\ \ \*2021.mxd")
analysis = arcpy.CopyFeatures_management('Splice Point', r'C:\ \ \ \ArcGIS\Exports\itemSplice.shp')
print(analysis)

Firstly, the following line isn't doing anything (I'm assuming you've redacted your path, because it's nonsense):

arcpy.mapping.MapDocument(r"P:\ \ \*2021.mxd")

But, you need to set that equal to a variable, such as mxd, which it appears you do further in your post.

The following line,

analysis = arcpy.CopyFeatures_management('Splice Point', r'C:\ \ \ \ArcGIS\Exports\itemSplice.shp')

is going to error because it doesn't know anything about the mxd. You should list all of the layers in the mxd first. You also don't need to set it equal to the variable "analysis". If the layer name is equal to 'Splice Point', then call the CopyFeatures_management tool.

So, your code may look like this:

mxd = arcpy.mapping.MapDocument(r"P:\ \ \*2021.mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name == 'Splice Point':
        arcpy.CopyFeatures_management(lyr, r'C:\ \ \ ArcGIS\Exports\itemSplice.shp')
1
  • 2
    It seems here that the *2021.mxd may be intended to relate to many documents that are named similarly, that being the case iterating a list of files from os.listdir(r"P:\ \ \") with endswith("2021.mxd") might be what the OP is after. I would also suggest finding equality with either .upper() or .lower() of the layer name, it's a good habit to get into as python is a case sensitive language. Dec 21, 2021 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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