[GIS] Can arcpy.env.workspace be set multiple times in same script

arcpyworkspace

Is it possible to set arcpy.env.workspace multiple times in the same script to ListDataFeatures from different workspaces?

eg.

import arcpy


def readFromGdb(): 

arcpy.env.workspace = "C:/data/base.gdb"

gdbList = listFeatureClasses()

#do the work 

def readFromShp():

arcpy.env.workspace = "C:/data_shp/" 

shpList = listFeatureClasses()    
#do the work 

Best Answer

If 10.1+, you can use arcpy.da.Walk and not touch the global arcpy environments:

from os.path import join
import arcpy

def find_all_fcs(workspace):
    """ return list of all fcs """
    fcs = []
    for dirpath, dirnames, filenames in arcpy.da.Walk(
            workspace,
            topdown=True, 
            followlinks=False,
            datatype='FeatureClass',
            type="ALL"):

        for filename in filenames:
            # optional join for fullpath instead of just fc name
            fcs.append(join(dirpath, filename))

    return fcs  

Otherwise, I usually do this to avoid any surprises later on:

import arcpy

def find_all_fcs(workspace):
    prev_workspace = arcpy.env.workspace

    arcpy.env.workspace = workspace
    fcs = arcpy.ListFeatureClasses()

    arcpy.env.workspace = prev_workspace
    return fcs