2

I've created 1 script to perform 2 specific tasks but it not working correctly due to how the arcpy.env.workspace is set.

The first part of the script deletes and recreates the sub folders in "C:/EIS/SHP Files"

arcpy.env.workspace = "C:/EIS/SHP Files"

The second part of the script looks inside a particular sub folder in "C:/EIS/SHP Files/Master folder" and copies Pipe data back into the newly created folders

arcpy.env.workspace = "C:/EIS/SHP Files/Master Folder"

Problem: I can't reference 2 environments at once in a script. The script will give out an error message since it can't look in 2 paths at once. So how can we do this?

import sys
import platform
import imp
print "Importing arcpy... this may take a moment\n"
import arcpy
from arcpy import env

# Set workspace
arcpy.env.workspace = "C:/EIS/SHP Files"

# Creates the county folders
out_folder_path = "C:/EIS/SHP Files/"

# Set local variables
# List of folder values in load order
folderList = ["1","2","3","4","5"]

# Store all the folder names in a dictionary with the folder code as the  
# "key" and the folder description as the "value" (folderDict[code])
folderDict ={"1":"OH_ADAMS","2":"OH_ALLEN","3":"OH_ASHLAND","4":"OH_ASHTABULA","5":"OH_ATHENS"}

# Wayne Pipe
in_data_WayneMain =  "WayneMain.shp"
out_Geauga_WayneMain = "C:/EIS/SHP Files/OH_GEAUGA/WayneMain.shp"

# The Code below will utilize the [arcpy.CreateFolder_management] tool in 
# order to rebuild the directory for C:\EIS\SHP Files\.. County Folders

# Process: Add valid material types to the domain
# use a for loop to cycle through all the domain codes in the dictionary
for code in folderList:
    if arcpy.Exists(out_folder_path):
        arcpy.Delete_management(folderDict[code])
        arcpy.CreateFolder_management(out_folder_path, folderDict[code])

# The Code below will utilize the [arcpy.Copy_management] tool in order to   
# copy files from the C:\EIS\SHP Files\Master Folder to C:\EIS\SHP Files.. 
# County Folders

# Wayne Pipe (Copied) To => County Folders
arcpy.Copy_management(in_data_WayneMain, out_Geauga_WayneMain)
0

2 Answers 2

7

Reset your environment workspace as required:

workspace1 = "C:/EIS/SHP Files"
workspace2 = "C:/EIS/SHP Files/Master Folder"

arcpy.env.workspace = workspace1 
#Do the stuff that requires workspace1 as the environment workspace

arcpy.env.workspace = workspace2 
#Do the stuff that requires workspace2 as the environment workspace

arcpy.env.workspace = workspace1 
#Change it back to workspace1 if it's needed again.

Alternatively, use the workspace1 etc. variables instead of the environment settings to specify the relevant folders:

os.path.join(workspace1, in_data_WayneMain)
1

Since ArcGIS Pro 2.8, it is possible to use arcpy.EnvManager() in a with block to temporarily set a workspace environment.

This documentation page explains how it works: https://pro.arcgis.com/en/pro-app/latest/arcpy/classes/envmanager.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.