<SPAN class="comment token">#11/17/2016</SPAN>
<SPAN class="comment token">#</SPAN>
<SPAN class="comment token">#take all of the shapefiles in a directory and compress them into individual zip files</SPAN>
<SPAN class="comment token">#</SPAN>
<SPAN class="comment token">#########################################################################################</SPAN>
<SPAN class="comment token">#import workspace</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env
<SPAN class="keyword token">from</SPAN> os <SPAN class="keyword token">import</SPAN> path <SPAN class="keyword token">as</SPAN> p
<SPAN class="keyword token">import</SPAN> RemoveAddLayer
<SPAN class="keyword token">import</SPAN> zipfile
arcpy<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">ZipShapes</SPAN><SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">,</SPAN> outpath<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> path
shapes <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFeatureClasses<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#iterate through list of shapefiles</SPAN>
<SPAN class="keyword token">for</SPAN> shape <SPAN class="keyword token">in</SPAN> shapes<SPAN class="punctuation token">:</SPAN>
name <SPAN class="operator token">=</SPAN> p<SPAN class="punctuation token">.</SPAN>splitext<SPAN class="punctuation token">(</SPAN>shape<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">print</SPAN> name
zip_path <SPAN class="operator token">=</SPAN> p<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>out_path<SPAN class="punctuation token">,</SPAN> name <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.zip'</SPAN><SPAN class="punctuation token">)</SPAN>
zip <SPAN class="operator token">=</SPAN> zipfile<SPAN class="punctuation token">.</SPAN>ZipFile<SPAN class="punctuation token">(</SPAN>zip_path<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'w'</SPAN><SPAN class="punctuation token">,</SPAN> compression<SPAN class="operator token">=</SPAN>zipfile<SPAN class="punctuation token">.</SPAN>ZIP_DEFLATED<SPAN class="punctuation token">)</SPAN>
zip<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN>p<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">,</SPAN>shape<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> shape<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> f <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFiles<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'%s*'</SPAN> <SPAN class="operator token">%</SPAN>name<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> f<SPAN class="punctuation token">.</SPAN>endswith<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'.shp'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
zip<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN>p<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">,</SPAN>f<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>f<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">'All file written to %s'</SPAN> <SPAN class="operator token">%</SPAN>zip_path
zip<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN>
path <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"Z:\Jared\Python Scripts"</SPAN>
outpath <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"W:\Data"</SPAN>
ZipShapes<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">,</SPAN> outpath<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Only a novice at Python. Using ArcMap for Desktop 10.3. My ultimate objective is to send some shapefiles to a zip file using script. I ran the above based on someone's existing script.
1.) What is if __name__ == '__main__': doing in this scenario?
From what I understand you can use this when importing another Python script? That's why I have the "RemoveAddLayer" script as an import at the top. I may be completely wrong?
2.) The RemoveAddLayer script (that's imported at the top) is what I created to update Streets and Address Points layers. Should I save these to a folder in the script and then set that as the path variable in the ZipFiles2 script?
There was no error when I ran this but also no file was put into the outpath folder.
Edit: @Rebecca: Thanks, I took your advice and put the script in instead of the image.