Adding shapefile to multiple MXDs using ArcPy?











up vote
1
down vote

favorite












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




















  • 2




    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
    2 hours ago















up vote
1
down vote

favorite












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




















  • 2




    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
    2 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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 1 hour ago









PolyGeo

52.9k1779237




52.9k1779237










asked 2 hours ago









Rachel

263




263








  • 2




    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
    2 hours ago














  • 2




    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
    2 hours ago








2




2




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
2 hours ago




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
2 hours ago










2 Answers
2






active

oldest

votes

















up vote
2
down vote













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




























    up vote
    2
    down vote













    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
      1 hour ago












    • 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
      1 hour ago













    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',
    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








    up vote
    2
    down vote













    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

























      up vote
      2
      down vote













      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























        up vote
        2
        down vote










        up vote
        2
        down vote









        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 1 hour ago









        Michael Stimson

        20.9k22260




        20.9k22260
























            up vote
            2
            down vote













            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
              1 hour ago












            • 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
              1 hour ago

















            up vote
            2
            down vote













            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
              1 hour ago












            • 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
              1 hour ago















            up vote
            2
            down vote










            up vote
            2
            down vote









            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 1 hour ago

























            answered 1 hour ago









            ahmadhanb

            21.2k31950




            21.2k31950








            • 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
              1 hour ago












            • 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
              1 hour ago
















            • 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
              1 hour ago












            • 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
              1 hour ago










            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
            1 hour ago






            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
            1 hour ago














            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
            1 hour ago






            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
            1 hour ago




















            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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'