<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Having trouble replaceDataSource loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703845#M54489</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A couple of things:&lt;/P&gt;&lt;P&gt;1) ListFeatureClasses requires some arguments&amp;nbsp;&lt;A href="http://pro.arcgis.com/en/pro-app/arcpy/functions/listfeatureclasses.htm" style="color: #007ac2; background-color: #ffffff; font-weight: bold; text-decoration: none;"&gt;ListFeatureClasses&lt;/A&gt;&lt;SPAN style="color: #4d4d4d; background-color: #ffffff; font-weight: bold;"&gt;(wild_card, feature_type, feature_dataset),&amp;nbsp;&lt;/SPAN&gt;and you are not passing anything, so your shapefile list is empty.&lt;/P&gt;&lt;P&gt;2) As Joshua indicated, you are passing an empty string as the name. You need to alter to:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;SPAN style="background-color: #ffffff;"&gt;for shape in shapelist:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;BR style="background-color: #ffffff;" /&gt;&lt;SPAN style="background-color: #ffffff;"&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.replaceDataSource(r"\\path\Set1Test", "SHAPEFILE_WORKSPACE", shape)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;Best Regards,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;Jim&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 19 May 2017 13:06:17 GMT</pubDate>
    <dc:creator>JimCousins</dc:creator>
    <dc:date>2017-05-19T13:06:17Z</dc:date>
    <item>
      <title>Having trouble replaceDataSource loop</title>
      <link>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703843#M54487</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;In ArcGIS (using 10.4) I have my layout set up, shapefile A symbolized and labeled. What I need to do is print this layout to pdf, then change the source of shapefile A to the next in a &lt;BR /&gt;series of several hundred, have it look the same, change the title, and print the pdf. I'm trying to loop through each shapefile in a folder. The shapefiles are polygon. The legend stays the same because each shapefile has the same number of&lt;BR /&gt;polygons, symbolized the same way. The labels should stay the same, but move as the polygons they label change shape and position. The extent is constant.&lt;BR /&gt;This is easy to do interactively in ArcGIS: go into the Layer Properties, Source tab, Set Data Source... &lt;BR /&gt;In trying to do this in Python, I'm having difficulty. My code maybe isn't looping. It prints one map. I think the replaceDataSource {dataset_name} needs to be a variable, but I can't figure how to do that.&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: courier new,courier,monospace;"&gt;&amp;gt;&amp;gt;&amp;gt; import arcpy &amp;nbsp;&lt;BR /&gt;... import os &amp;nbsp;&lt;BR /&gt;... mxd = arcpy.mapping.MapDocument("CURRENT")&amp;nbsp; &amp;nbsp;&lt;BR /&gt;... df = arcpy.mapping.ListDataFrames(mxd, "*")[0]&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;... lyr = arcpy.mapping.ListLayers(mxd, "*", df)[3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;... output_dir = r"\\path\PDFs"&amp;nbsp; &amp;nbsp;&lt;BR /&gt;... arcpy.env.workspace = r"\\path\Set1Test" &lt;BR /&gt;... shapelist = arcpy.ListFeatureClasses()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;... for shape in shapelist:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.replaceDataSource(r"\\path\Set1Test", "SHAPEFILE_WORKSPACE", "") &lt;BR /&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TextElement = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "MapTitleText")[0] &amp;nbsp;&lt;BR /&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TextElement.text = lyr.datasetName&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.ExportToPDF(mxd, r"\\path\PDFs\Map " + lyr.datasetName)&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 May 2017 20:43:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703843#M54487</guid>
      <dc:creator>RaleighMyers</dc:creator>
      <dc:date>2017-05-18T20:43:56Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble replaceDataSource loop</title>
      <link>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703844#M54488</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You are looping over your shape files, assuming there are shape files to loop over, but you are not doing anything each shape file as you loop over them.&amp;nbsp; Looking at your &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;replaceDataSource&lt;/SPAN&gt; syntax, you are passing an empty string as the &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;dataset_name&lt;/SPAN&gt;.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 May 2017 12:58:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703844#M54488</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2017-05-19T12:58:47Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble replaceDataSource loop</title>
      <link>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703845#M54489</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A couple of things:&lt;/P&gt;&lt;P&gt;1) ListFeatureClasses requires some arguments&amp;nbsp;&lt;A href="http://pro.arcgis.com/en/pro-app/arcpy/functions/listfeatureclasses.htm" style="color: #007ac2; background-color: #ffffff; font-weight: bold; text-decoration: none;"&gt;ListFeatureClasses&lt;/A&gt;&lt;SPAN style="color: #4d4d4d; background-color: #ffffff; font-weight: bold;"&gt;(wild_card, feature_type, feature_dataset),&amp;nbsp;&lt;/SPAN&gt;and you are not passing anything, so your shapefile list is empty.&lt;/P&gt;&lt;P&gt;2) As Joshua indicated, you are passing an empty string as the name. You need to alter to:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;SPAN style="background-color: #ffffff;"&gt;for shape in shapelist:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;BR style="background-color: #ffffff;" /&gt;&lt;SPAN style="background-color: #ffffff;"&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.replaceDataSource(r"\\path\Set1Test", "SHAPEFILE_WORKSPACE", shape)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;Best Regards,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;Jim&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 May 2017 13:06:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703845#M54489</guid>
      <dc:creator>JimCousins</dc:creator>
      <dc:date>2017-05-19T13:06:17Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble replaceDataSource loop</title>
      <link>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703846#M54490</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Technically all the arguments are optional so this would return something even with no arguments(see the example code in the help), as long as the workspace environment was valid and contained feature classes.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 May 2017 14:13:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703846#M54490</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2017-05-19T14:13:22Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble replaceDataSource loop</title>
      <link>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703847#M54491</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;ListFeatureClasses requires no parameters, in which case everything is returned... check the code example in the link&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 May 2017 14:22:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703847#M54491</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2017-05-19T14:22:29Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble replaceDataSource loop</title>
      <link>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703848#M54492</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Point taken.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 May 2017 14:24:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703848#M54492</guid>
      <dc:creator>JimCousins</dc:creator>
      <dc:date>2017-05-19T14:24:50Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble replaceDataSource loop</title>
      <link>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703849#M54493</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, the ListFeatureClasses() returns a list. I have 11 of the shapefiles in a test directory. Here's what &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;print shapelist&lt;/SPAN&gt; returns after running the code:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier,monospace;"&gt;[u'Set1_Plan1.shp', u'Set1_Plan10.shp', u'Set1_Plan11.shp', u'Set1_Plan2.shp', u'Set1_Plan3.shp', u'Set1_Plan4.shp', u'Set1_Plan5.shp', u'Set1_Plan6.shp', u'Set1_Plan7.shp', u'Set1_Plan8.shp', u'Set1_Plan9.shp']&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 May 2017 14:31:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703849#M54493</guid>
      <dc:creator>RaleighMyers</dc:creator>
      <dc:date>2017-05-19T14:31:25Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble replaceDataSource loop</title>
      <link>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703850#M54494</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That means when you use the placeholder variable shape in the loop, it should be the name of each of those shapefile in turn.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is what you have:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Courier New;"&gt;lyr.replaceDataSource(r"\\path\Set1Test", "SHAPEFILE_WORKSPACE", "") &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Courier New;"&gt;This is what it should be(As Jim previously noted):&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Courier New;"&gt;lyr.replaceDataSource(r"\\path\Set1Test", "SHAPEFILE_WORKSPACE", shape) &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: Courier New;"&gt;Also wouldn't hurt to refresh the active view after changing the source each time with arcpy.RefreshActiveView()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 May 2017 14:44:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703850#M54494</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2017-05-19T14:44:38Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble replaceDataSource loop</title>
      <link>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703851#M54495</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;TABLE&gt;&lt;TBODY data-addlink-disabled="false" data-canpost="true" data-cansee="false" data-comments-unavailable="false" data-remaining-comments-count="0"&gt;&lt;TR class=""&gt;&lt;TD class=""&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/TD&gt;&lt;TD class=""&gt;&lt;DIV class="" style="display: block;"&gt;&lt;SPAN class=""&gt;Yes! I have tried &lt;CODE&gt;lyr.replaceDataSource(r"\\path\Set1\Set1Test", "SHAPEFILE_WORKSPACE", shape)&lt;/CODE&gt; . It produces an "Unexpected error." I think the dataset name needs to be a variable, but I haven't been able to figure how to do that.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class="" style="display: block;"&gt;&lt;SPAN class=""&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class="" style="display: block;"&gt;&lt;SPAN class=""&gt;Per your suggestion, I added &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;RefreshActiveView()&lt;/SPAN&gt; after &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;replaceDataSource&lt;/SPAN&gt; and ran the code. It refreshed 10 times, but it's not replacing the data source with the next one. It keeps the first one each time and only prints out one pdf. (Or maybe two pdfs. It created one with an 11:05 timestamp, then the timestamp was 11:06)&lt;BR /&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 May 2017 14:54:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703851#M54495</guid>
      <dc:creator>RaleighMyers</dc:creator>
      <dc:date>2017-05-19T14:54:50Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble replaceDataSource loop</title>
      <link>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703852#M54496</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Any more information on the error you are getting?&amp;nbsp; Are you sure you are creating the layer object for the correct layer in your map document?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Without more information its hard to know why exactly the script is failing.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also see this thread and make sure relative paths are not on when replacing data sources&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://gis.stackexchange.com/questions/40549/replacing-layer-data-sources-using-replacedatasource-of-arcpy?rq=1"&gt;https://gis.stackexchange.com/questions/40549/replacing-layer-data-sources-using-replacedatasource-of-arcpy?rq=1&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 22 May 2017 18:53:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/having-trouble-replacedatasource-loop/m-p/703852#M54496</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2017-05-22T18:53:36Z</dc:date>
    </item>
  </channel>
</rss>

