Adding shapefile to multiple MXDs using ArcPy?












1














My ArcMap verison is 10.6. The purpose is to add a wetland shapefile to multiple MXDs. It doesn't work and the error said the addLayer is not defined. My script is below.



Does anyone have an idea?



import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktopExampleWetland.shp"
wetland = r"C:UsersRachelDesktop"

mxd_list = arcpy.ListFiles("*.mxd")

for mxd in mxd_list:

current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))

dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)

current_mxd.save()
print("done")

del mxd_list









share|improve this question




















  • 3




    You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists.
    – ahmadhanb
    Dec 6 at 1:22
















1














My ArcMap verison is 10.6. The purpose is to add a wetland shapefile to multiple MXDs. It doesn't work and the error said the addLayer is not defined. My script is below.



Does anyone have an idea?



import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktopExampleWetland.shp"
wetland = r"C:UsersRachelDesktop"

mxd_list = arcpy.ListFiles("*.mxd")

for mxd in mxd_list:

current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))

dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)

current_mxd.save()
print("done")

del mxd_list









share|improve this question




















  • 3




    You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists.
    – ahmadhanb
    Dec 6 at 1:22














1












1








1







My ArcMap verison is 10.6. The purpose is to add a wetland shapefile to multiple MXDs. It doesn't work and the error said the addLayer is not defined. My script is below.



Does anyone have an idea?



import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktopExampleWetland.shp"
wetland = r"C:UsersRachelDesktop"

mxd_list = arcpy.ListFiles("*.mxd")

for mxd in mxd_list:

current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))

dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)

current_mxd.save()
print("done")

del mxd_list









share|improve this question















My ArcMap verison is 10.6. The purpose is to add a wetland shapefile to multiple MXDs. It doesn't work and the error said the addLayer is not defined. My script is below.



Does anyone have an idea?



import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktopExampleWetland.shp"
wetland = r"C:UsersRachelDesktop"

mxd_list = arcpy.ListFiles("*.mxd")

for mxd in mxd_list:

current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))

dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)

current_mxd.save()
print("done")

del mxd_list






arcpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 6 at 1:59









PolyGeo

53.1k1779238




53.1k1779238










asked Dec 6 at 1:01









Rachel

293




293








  • 3




    You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists.
    – ahmadhanb
    Dec 6 at 1:22














  • 3




    You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists.
    – ahmadhanb
    Dec 6 at 1:22








3




3




You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists.
– ahmadhanb
Dec 6 at 1:22




You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists.
– ahmadhanb
Dec 6 at 1:22










2 Answers
2






active

oldest

votes


















3














There are many issues in your code:




  1. You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists

  2. No indent after for loop


Here is a working code that I used



import arcpy,os
arcpy.env.workspace = r"C:UsersRachelDesktop"
ws = arcpy.env.workspace
wetland = r"C:UsersRachelDesktopExampleWetland.shp"

mxd_list = arcpy.ListFiles("*.mxd")

for mxd in mxd_list:

current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))

dFrame = arcpy.mapping.ListDataFrames(current_mxd)[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)

current_mxd.save()
print("done")

del mxd_list


The above code assumes that the mxd files are stored inside r"C:UsersRachelDesktop" workspace.






share|improve this answer



















  • 1




    I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
    – Michael Stimson
    Dec 6 at 1:40












  • You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
    – ahmadhanb
    Dec 6 at 1:43





















2














You haven't got any indentation in your code sample, that may just be this version of the code has been badly formatted but as it reads the iteration for mxd in mxd_list: has no instructions and will cause an error. In python indentation is crucial to understanding what the code does.



@ahmadhanb is correct, you're transposing your workspace and wetland:



import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktop"
wetland = r"C:UsersRachelDesktopExampleWetland.shp"

mxd_list = arcpy.ListFiles("*.mxd")

for mxd in mxd_list:

current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))

dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)

current_mxd.save()
print("done")

del mxd_list


However I would dissuade you from using your desktop as your workspace. User folders have funny permissions and can be subject to quota management; please consider making a folder at the root level (call it GIS perhaps, example C:GIS) and work from there to avoid potential problems. By all means add a shortcut to it on your desktop for ease of access.






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "79"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f305151%2fadding-shapefile-to-multiple-mxds-using-arcpy%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    There are many issues in your code:




    1. You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists

    2. No indent after for loop


    Here is a working code that I used



    import arcpy,os
    arcpy.env.workspace = r"C:UsersRachelDesktop"
    ws = arcpy.env.workspace
    wetland = r"C:UsersRachelDesktopExampleWetland.shp"

    mxd_list = arcpy.ListFiles("*.mxd")

    for mxd in mxd_list:

    current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))

    dFrame = arcpy.mapping.ListDataFrames(current_mxd)[0]
    addLayer = arcpy.mapping.Layer(wetland)
    arcpy.mapping.AddLayer(dFrame, addLayer)

    current_mxd.save()
    print("done")

    del mxd_list


    The above code assumes that the mxd files are stored inside r"C:UsersRachelDesktop" workspace.






    share|improve this answer



















    • 1




      I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
      – Michael Stimson
      Dec 6 at 1:40












    • You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
      – ahmadhanb
      Dec 6 at 1:43


















    3














    There are many issues in your code:




    1. You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists

    2. No indent after for loop


    Here is a working code that I used



    import arcpy,os
    arcpy.env.workspace = r"C:UsersRachelDesktop"
    ws = arcpy.env.workspace
    wetland = r"C:UsersRachelDesktopExampleWetland.shp"

    mxd_list = arcpy.ListFiles("*.mxd")

    for mxd in mxd_list:

    current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))

    dFrame = arcpy.mapping.ListDataFrames(current_mxd)[0]
    addLayer = arcpy.mapping.Layer(wetland)
    arcpy.mapping.AddLayer(dFrame, addLayer)

    current_mxd.save()
    print("done")

    del mxd_list


    The above code assumes that the mxd files are stored inside r"C:UsersRachelDesktop" workspace.






    share|improve this answer



















    • 1




      I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
      – Michael Stimson
      Dec 6 at 1:40












    • You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
      – ahmadhanb
      Dec 6 at 1:43
















    3












    3








    3






    There are many issues in your code:




    1. You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists

    2. No indent after for loop


    Here is a working code that I used



    import arcpy,os
    arcpy.env.workspace = r"C:UsersRachelDesktop"
    ws = arcpy.env.workspace
    wetland = r"C:UsersRachelDesktopExampleWetland.shp"

    mxd_list = arcpy.ListFiles("*.mxd")

    for mxd in mxd_list:

    current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))

    dFrame = arcpy.mapping.ListDataFrames(current_mxd)[0]
    addLayer = arcpy.mapping.Layer(wetland)
    arcpy.mapping.AddLayer(dFrame, addLayer)

    current_mxd.save()
    print("done")

    del mxd_list


    The above code assumes that the mxd files are stored inside r"C:UsersRachelDesktop" workspace.






    share|improve this answer














    There are many issues in your code:




    1. You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists

    2. No indent after for loop


    Here is a working code that I used



    import arcpy,os
    arcpy.env.workspace = r"C:UsersRachelDesktop"
    ws = arcpy.env.workspace
    wetland = r"C:UsersRachelDesktopExampleWetland.shp"

    mxd_list = arcpy.ListFiles("*.mxd")

    for mxd in mxd_list:

    current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))

    dFrame = arcpy.mapping.ListDataFrames(current_mxd)[0]
    addLayer = arcpy.mapping.Layer(wetland)
    arcpy.mapping.AddLayer(dFrame, addLayer)

    current_mxd.save()
    print("done")

    del mxd_list


    The above code assumes that the mxd files are stored inside r"C:UsersRachelDesktop" workspace.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 6 at 1:44

























    answered Dec 6 at 1:38









    ahmadhanb

    21.6k31951




    21.6k31951








    • 1




      I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
      – Michael Stimson
      Dec 6 at 1:40












    • You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
      – ahmadhanb
      Dec 6 at 1:43
















    • 1




      I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
      – Michael Stimson
      Dec 6 at 1:40












    • You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
      – ahmadhanb
      Dec 6 at 1:43










    1




    1




    I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
    – Michael Stimson
    Dec 6 at 1:40






    I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
    – Michael Stimson
    Dec 6 at 1:40














    You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
    – ahmadhanb
    Dec 6 at 1:43






    You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
    – ahmadhanb
    Dec 6 at 1:43















    2














    You haven't got any indentation in your code sample, that may just be this version of the code has been badly formatted but as it reads the iteration for mxd in mxd_list: has no instructions and will cause an error. In python indentation is crucial to understanding what the code does.



    @ahmadhanb is correct, you're transposing your workspace and wetland:



    import arcpy,os
    arcpy.env.workspace = ws = r"C:UsersRachelDesktop"
    wetland = r"C:UsersRachelDesktopExampleWetland.shp"

    mxd_list = arcpy.ListFiles("*.mxd")

    for mxd in mxd_list:

    current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))

    dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
    addLayer = arcpy.mapping.Layer(wetland)
    arcpy.mapping.AddLayer(dFrame, addLayer)

    current_mxd.save()
    print("done")

    del mxd_list


    However I would dissuade you from using your desktop as your workspace. User folders have funny permissions and can be subject to quota management; please consider making a folder at the root level (call it GIS perhaps, example C:GIS) and work from there to avoid potential problems. By all means add a shortcut to it on your desktop for ease of access.






    share|improve this answer


























      2














      You haven't got any indentation in your code sample, that may just be this version of the code has been badly formatted but as it reads the iteration for mxd in mxd_list: has no instructions and will cause an error. In python indentation is crucial to understanding what the code does.



      @ahmadhanb is correct, you're transposing your workspace and wetland:



      import arcpy,os
      arcpy.env.workspace = ws = r"C:UsersRachelDesktop"
      wetland = r"C:UsersRachelDesktopExampleWetland.shp"

      mxd_list = arcpy.ListFiles("*.mxd")

      for mxd in mxd_list:

      current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))

      dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
      addLayer = arcpy.mapping.Layer(wetland)
      arcpy.mapping.AddLayer(dFrame, addLayer)

      current_mxd.save()
      print("done")

      del mxd_list


      However I would dissuade you from using your desktop as your workspace. User folders have funny permissions and can be subject to quota management; please consider making a folder at the root level (call it GIS perhaps, example C:GIS) and work from there to avoid potential problems. By all means add a shortcut to it on your desktop for ease of access.






      share|improve this answer
























        2












        2








        2






        You haven't got any indentation in your code sample, that may just be this version of the code has been badly formatted but as it reads the iteration for mxd in mxd_list: has no instructions and will cause an error. In python indentation is crucial to understanding what the code does.



        @ahmadhanb is correct, you're transposing your workspace and wetland:



        import arcpy,os
        arcpy.env.workspace = ws = r"C:UsersRachelDesktop"
        wetland = r"C:UsersRachelDesktopExampleWetland.shp"

        mxd_list = arcpy.ListFiles("*.mxd")

        for mxd in mxd_list:

        current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))

        dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
        addLayer = arcpy.mapping.Layer(wetland)
        arcpy.mapping.AddLayer(dFrame, addLayer)

        current_mxd.save()
        print("done")

        del mxd_list


        However I would dissuade you from using your desktop as your workspace. User folders have funny permissions and can be subject to quota management; please consider making a folder at the root level (call it GIS perhaps, example C:GIS) and work from there to avoid potential problems. By all means add a shortcut to it on your desktop for ease of access.






        share|improve this answer












        You haven't got any indentation in your code sample, that may just be this version of the code has been badly formatted but as it reads the iteration for mxd in mxd_list: has no instructions and will cause an error. In python indentation is crucial to understanding what the code does.



        @ahmadhanb is correct, you're transposing your workspace and wetland:



        import arcpy,os
        arcpy.env.workspace = ws = r"C:UsersRachelDesktop"
        wetland = r"C:UsersRachelDesktopExampleWetland.shp"

        mxd_list = arcpy.ListFiles("*.mxd")

        for mxd in mxd_list:

        current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))

        dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
        addLayer = arcpy.mapping.Layer(wetland)
        arcpy.mapping.AddLayer(dFrame, addLayer)

        current_mxd.save()
        print("done")

        del mxd_list


        However I would dissuade you from using your desktop as your workspace. User folders have funny permissions and can be subject to quota management; please consider making a folder at the root level (call it GIS perhaps, example C:GIS) and work from there to avoid potential problems. By all means add a shortcut to it on your desktop for ease of access.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 6 at 1:39









        Michael Stimson

        21k22260




        21k22260






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f305151%2fadding-shapefile-to-multiple-mxds-using-arcpy%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Quarter-circle Tiles

            build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

            Mont Emei