0

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
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Aug 3, 2016 at 13:16
9
  • Since this is purely a python question with no GIS components, you should ask it on stackoverflow. Commented Aug 3, 2016 at 13:24
  • Well its about setting the env,workspace within Arc. Commented Aug 3, 2016 at 13:25
  • Yeah, I didn't notice. You should add the 'arcpy' tag to your question. Commented 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. Commented Aug 3, 2016 at 13:31
  • Ive changed the tag to arcpy to avoid confusion. Commented Aug 3, 2016 at 13:33

2 Answers 2

2

replace

folder = arcpy.CreateFolder_management(location, date)

with

folder = str(arcpy.CreateFolder_management(location, date)) + "\\"
answered Aug 3, 2016 at 17:00
2
  • Unfortunately that didn't work. It fails later down the code when trying to locate files within "folder". Commented Aug 4, 2016 at 8:03
  • updated to include backslash at end of pathname Commented Aug 4, 2016 at 12:10
0

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\August20160804円_August_2016\
answered Aug 4, 2016 at 8:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.