<?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: Using shutil to copy rasters in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-shutil-to-copy-rasters/m-p/402282#M31675</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks both of you for your help. I tried the copy raster method and took much longer than the shutil. It works well for the formats I am exporting (.asc and .tif). Here is the code if anyone is interested.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import os
import shutil

source_dir = r"source\dir"
arcpy.env.workspace = source_dir
table = r"selection_table"
outDirBase = r"output\folder"
if not os.path.exists(outDirBase):
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.mkdir(outDirBase)

def GetList(table):
&amp;nbsp;&amp;nbsp;&amp;nbsp; name_list = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; s_curs = arcpy.SearchCursor(table)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in s_curs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name = row.Name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name_final = "XX_"+name.split("_")[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name_list.append(name_final)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return name_list

nts_list = GetList(table)

dir_list = [("bare_earth_DEM_ascii","be"),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ("bare_earth_hillshade_tif","be"),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ("full_feature_DSM_ascii","ff"),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ("full_feature_hillshade_tif","ff"),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ("intensity_image_tif","ii")]
# Get directory name and associated file prefix from tuple list
for directory,pref in dir_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create a variable to hold the full path to the export directory
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_dir = os.path.join(outDirBase,directory)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # If this folder doesn't exist, create it
&amp;nbsp;&amp;nbsp;&amp;nbsp; if not os.path.exists(new_dir):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; os.mkdir(new_dir)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create a variable to hold the full path to the source directory
&amp;nbsp;&amp;nbsp;&amp;nbsp; in_dir = os.path.join(source_dir,directory)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Loop through grid locations in the list of selected grids
&amp;nbsp;&amp;nbsp;&amp;nbsp; for nts in nts_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Assign the associated prefix to the current grid in the list
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var_in = pref+nts[2:]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create a list of files in the source directory to test against
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; file_list = os.listdir(in_dir)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Loop through all files in the source directory
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in file_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # If the current file matches the
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if filename.startswith(var_in):
&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; print "copying {0}".format(filename)
&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; shutil.copy2(os.path.join(in_dir,filename),os.path.join(new_dir,filename))&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 18:20:21 GMT</pubDate>
    <dc:creator>MathewCoyle</dc:creator>
    <dc:date>2021-12-11T18:20:21Z</dc:date>
    <item>
      <title>Using shutil to copy rasters</title>
      <link>https://community.esri.com/t5/python-questions/using-shutil-to-copy-rasters/m-p/402279#M31672</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Is there any problems with doing this? I take all files associated with a asc or tif and use shutil.copy2. They seem to come out alright but just wondering if I am missing anything behind the scenes?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I went this route as I was having trouble using the ListRasters tool efficiently, it would take minutes just to list the rasters I want in my source directory of 14K rasters. I switched to os.listdir to get all the files matching the names I want and is much faster.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Jun 2012 19:41:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-shutil-to-copy-rasters/m-p/402279#M31672</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-06-14T19:41:56Z</dc:date>
    </item>
    <item>
      <title>Re: Using shutil to copy rasters</title>
      <link>https://community.esri.com/t5/python-questions/using-shutil-to-copy-rasters/m-p/402280#M31673</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; just wondering if I am missing anything behind the scenes?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One thing to consider: raster image files often have ancillary files associated with them (auxiliary files, metadata files, pyramids) that would be left behind by an OS copy. The Copy Raster tool would always take those ancillary files along. You may be able to work around this by doing a wildcard copy, since these ancillary files are named the same as the raster.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In the case of .tif files, the TIFF image format includes internal georeferencing and sometimes even coordinate system, so you may not have an issue. If you lose the coordinate system in the system copy, you could reinstate it on the output using the Define Projection tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So I guess my advice is - check your output to make sure all the ancillary information you need about the raster traveled with the OS copy.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To avoid someone getting into trouble I should mention that copying a an Esri grid format raster with shutil would be a bad idea because grids include the information in the grid folder plus *some* of the files in the associated "info" folder. Copy Raster should be used with grids so you get the INFO tables along for the ride.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Jun 2012 01:30:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-shutil-to-copy-rasters/m-p/402280#M31673</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2012-06-15T01:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: Using shutil to copy rasters</title>
      <link>https://community.esri.com/t5/python-questions/using-shutil-to-copy-rasters/m-p/402281#M31674</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You could comine os.listdir with Copy Raster.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Jun 2012 07:09:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-shutil-to-copy-rasters/m-p/402281#M31674</guid>
      <dc:creator>FabianBlau</dc:creator>
      <dc:date>2012-06-15T07:09:59Z</dc:date>
    </item>
    <item>
      <title>Re: Using shutil to copy rasters</title>
      <link>https://community.esri.com/t5/python-questions/using-shutil-to-copy-rasters/m-p/402282#M31675</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks both of you for your help. I tried the copy raster method and took much longer than the shutil. It works well for the formats I am exporting (.asc and .tif). Here is the code if anyone is interested.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import os
import shutil

source_dir = r"source\dir"
arcpy.env.workspace = source_dir
table = r"selection_table"
outDirBase = r"output\folder"
if not os.path.exists(outDirBase):
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.mkdir(outDirBase)

def GetList(table):
&amp;nbsp;&amp;nbsp;&amp;nbsp; name_list = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; s_curs = arcpy.SearchCursor(table)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in s_curs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name = row.Name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name_final = "XX_"+name.split("_")[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name_list.append(name_final)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return name_list

nts_list = GetList(table)

dir_list = [("bare_earth_DEM_ascii","be"),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ("bare_earth_hillshade_tif","be"),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ("full_feature_DSM_ascii","ff"),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ("full_feature_hillshade_tif","ff"),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ("intensity_image_tif","ii")]
# Get directory name and associated file prefix from tuple list
for directory,pref in dir_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create a variable to hold the full path to the export directory
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_dir = os.path.join(outDirBase,directory)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # If this folder doesn't exist, create it
&amp;nbsp;&amp;nbsp;&amp;nbsp; if not os.path.exists(new_dir):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; os.mkdir(new_dir)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create a variable to hold the full path to the source directory
&amp;nbsp;&amp;nbsp;&amp;nbsp; in_dir = os.path.join(source_dir,directory)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Loop through grid locations in the list of selected grids
&amp;nbsp;&amp;nbsp;&amp;nbsp; for nts in nts_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Assign the associated prefix to the current grid in the list
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var_in = pref+nts[2:]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create a list of files in the source directory to test against
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; file_list = os.listdir(in_dir)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Loop through all files in the source directory
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in file_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # If the current file matches the
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if filename.startswith(var_in):
&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; print "copying {0}".format(filename)
&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; shutil.copy2(os.path.join(in_dir,filename),os.path.join(new_dir,filename))&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:20:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-shutil-to-copy-rasters/m-p/402282#M31675</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T18:20:21Z</dc:date>
    </item>
  </channel>
</rss>

