[GIS] Python: how to set a different output folder

arcpypythonworkspace

I want to transform some vectors to rasters in my folder but I have difficulties setting the output folder. The following code only allows me to output to the original database. I hope to output all results to the same folder instead of in their original database.

import arcpy
import os

workspace = "D:\Jiawei default download"
feature_classes = []
walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")
for dirpath, dirnames, filenames in walk:
    if "Subbasin" in filenames:
        filenames.remove('Subbasin')
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print feature_classes

for item in feature_classes[98:]:
   inFeatures = item
   valField = "wtdepaprjunmin"
   outRaster = item+"_r"
   print "processing "+item
# Execute PolygonToRaster
   arcpy.PolygonToRaster_conversion(inFeatures, valField, outRaster) 

Best Answer

You set a workspace, so any outputs from the script will default to that location unless you explicitly put them somewhere else. Create a folder for the outputs, then write your output rasters to that folder.

import arcpy
import os

workspace = "D:\Jiawei default download"
feature_classes = []
outputFolder = r'c:\outputfolder'
walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")
for dirpath, dirnames, filenames in walk:
    if "Subbasin" in filenames:
        filenames.remove('Subbasin')
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print feature_classes

for item in feature_classes[98:]:
   inFeatures = item
   valField = "wtdepaprjunmin"
   outRaster = outputFolder+item+"_r"
   print "processing "+item
# Execute PolygonToRaster
   arcpy.PolygonToRaster_conversion(inFeatures, valField, outRaster)