# Creating a folder and setting as workspace using ArcPy?

I am trying to create a folder and setting that as the workspace for the rest of the script. However I am not sure why it isn't working.

When I hard wire in a location, the env.workspace can be set to that variable. See script below:

import arcpy, datetime

today = datetime.date.today()
date = today.strftime("%Y%m%d_%B_%Y")

folder = r"path\to\folder\\"
arcpy.env.workspace = folder


However when I create a folder using CreateFolder tool, and set that variable as the env.workspace, I keep getting an error. The script doesn't run.

import arcpy, datetime

today = datetime.date.today()
date = today.strftime("%Y%m%d_%B_%Y")

location = r"path\to\location\\"
folder = arcpy.CreateFolder_management(location, date)

arcpy.env.workspace = folder

• Since this is purely a python question with no GIS components, you should ask it on stackoverflow. Aug 3, 2016 at 13:24
• Well its about setting the env,workspace within Arc. Aug 3, 2016 at 13:25
• Yeah, I didn't notice. You should add the 'arcpy' tag to your question. Aug 3, 2016 at 13:27
• Hasan, I think you can find these types of questions all over GIS.SE. A lot of times the answer is a problem with the code and not with GIS component of the code. However, people still ask and answer here for these types of questions. I can see it going both ways. Aug 3, 2016 at 13:31
• Ive changed the tag to arcpy to avoid confusion. Aug 3, 2016 at 13:33

replace

folder = arcpy.CreateFolder_management(location, date)


with

folder = str(arcpy.CreateFolder_management(location, date)) + "\\"

• Unfortunately that didn't work. It fails later down the code when trying to locate files within "folder". Aug 4, 2016 at 8:03
• updated to include backslash at end of pathname Aug 4, 2016 at 12:10

I managed to get it to work with the following, by adding backslashes to the variable "folder".

#Import module
import arcpy, datetime

#Set the date for automatic folder creation
today = datetime.date.today()
date = today.strftime("%Y%m%d_%B_%Y")

#Create and set the workspace
location = r"C:\Test\August"
newfolder = arcpy.CreateFolder_management(location, date)
print "Workspace created"

#Create a variable called folder to be the workspace
folder = str(str(newfolder) + "\\")
arcpy.env.workspace = folder
print folder
C:\Test\August\20160804_August_2016\