2

I want to connect an existing .lyr file to a variable defined as lyr2.

lyr2 = arcpy.mapping.Layer(r"C:/Iso100Red.lyr")

Although it is a .lyr file, I get this Error code:

Runtime error <type 'exceptions.ValueError'>: Object: CreateObject Layer invalid data source

What could be the problem here?

5
  • might be because '/' is not the valid path seperator for windows. You have to use '\', eg. "C:\Iso100Red.lyr". See here: en.wikipedia.org/wiki/Path_%28computing%29 – Curlew Aug 7 '13 at 18:35
  • 2
    Forward slash is fine for Python on Windows. I thought using raw (r) with forward slash (it is designed to avoid backslash being treated as an escape character) may have been the problem but I just tested and it worked fine at ArcGIS 10.2 with Python 2.7.3. – PolyGeo Aug 7 '13 at 22:20
  • The error message isn't even that cryptic. Is says Layer invalid data source. This means the layer data is not valid. That means that when attempting to open the .lyr file ArcGIS was either unable to access the file or the data within was not valid Layer information. – Jason Scheirer Aug 7 '13 at 22:56
  • I created the file straight out of ArcGis.So the data within should be ok. – gesi666 Aug 8 '13 at 12:46
  • to rephrase @PolyGeo: with r use backslash paths: r"C:\Iso100Red.lyr", without r use foreslash: "C:/Iso100Red.lyr". r stands for "raw". – matt wilkie Sep 26 '19 at 17:18
4

The file C:\Iso100Red.lyr does not exist or isn't a valid layer file.

0
-1

You need to call out the layer when outside of an mxd. Please see below:

for lyr in arcpy.mapping.ListLayers(mapDoc):
    if lyr.name in ["Parcel Inspection Requirement", "Plat Boundary", "Red Plat Boundary"]:
        lyr.definitionQuery = '"JobConfirmationNumber" = \'{0}\''.format(conNumber)
        print "Definition Query 1"
    if lyr.name == "Grey Plat Boundary":
        lyr.definitionQuery = '"JobConfirmationNumber" <> \'{0}\' OR "JobConfirmationNumber" IS NULL'.format(conNumber)
        print "Definition Query 2"

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.