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


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 
  • 1
    That should be fine, you can definitely change a workspace env setting within the script. It should be noted though that you need to be careful when working with this within functions though as in my experience it doesn't follow what you might expect as the normal python behavior regarding in-function scope of variables, so calling one function from another could mess with the env setting and you'd have to go back and check it or re-set it if you needed to continue again in the original calling function.
    – John
    Apr 16, 2014 at 22:36
  • I've done it many times, one workspace for list rasters then another for list feature classes. Just as John said, ensure your workspace is set to the appropriate path immediately prior to listing every time you want to list feature classes either by setting implicitly or by comparing the value to what you expect it to be. Apr 17, 2014 at 4:17
  • @MichaelMiles-Stimson: It seems to retain the most recent env.workspace definition, regardless of whether it's inside/outside a function, and even between one script and another. Am I assessing that correctly?
    – joechoj
    Nov 4, 2016 at 21:30
  • 1
    That's my experience @joechoj, the env.workspace is global so is not just limited to the scope of the function though I wouldn't like to count on it being so just in case the scope changes in a later release or service pack. I prefer to avoid changing the workspace in a function and if it's absolutely necessary to do so store the current value and reset before exiting (make sure to account for all try: blocks). Nov 7, 2016 at 4:18

1 Answer 1


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(

        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
  • +1 for resetting the workspace at the end of the function
    – nmpeterson
    Apr 17, 2014 at 14:11
  • To reset the workspace reliably, it needs to be in a finally block or a context manager.
    – jpmc26
    Oct 3, 2017 at 3:00

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.