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.Hasan Mustafa– Hasan Mustafa2016年08月03日 13:24:01 +00:00Commented Aug 3, 2016 at 13:24
-
Well its about setting the env,workspace within Arc.MacroZED– MacroZED2016年08月03日 13:25:48 +00:00Commented Aug 3, 2016 at 13:25
-
Yeah, I didn't notice. You should add the 'arcpy' tag to your question.Hasan Mustafa– Hasan Mustafa2016年08月03日 13:27:22 +00:00Commented 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.Barrett– Barrett2016年08月03日 13:31:20 +00:00Commented Aug 3, 2016 at 13:31
-
Ive changed the tag to arcpy to avoid confusion.MacroZED– MacroZED2016年08月03日 13:33:11 +00:00Commented Aug 3, 2016 at 13:33
2 Answers 2
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".MacroZED– MacroZED2016年08月04日 08:03:06 +00:00Commented Aug 4, 2016 at 8:03
-
updated to include backslash at end of pathnamecartoscience– cartoscience2016年08月04日 12:10:41 +00:00Commented 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\August20160804円_August_2016\