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.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)

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)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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