Select to view content in your preferred language

How to implement os.getcwd() in Pro 2.1.2?

2440
12
Jump to solution
06-04-2018 01:18 PM
ThomasColson
MVP Frequent Contributor

When attaching the following to a tool box in Pro 2.1.2 and running it: 

import os
import errno
import arcpy
curr = dirpath = os.getcwd()
directories = ['Data','Data//GPS','Data//Working','Data//Tabular','Products','Documents', 'Documents//Pics_Graphics']
basedirectory = curr +'//'
for i in range (len (directories)):
 newDir = basedirectory + directories[i]
 try:
 os.makedirs(newDir)
 except OSError as exception:
 if exception.errno != errno.EEXIST:
 raise
 else:
 print ("\nBE CAREFUL! Directory %s already exists." % newDir)

I get the following error: 

Start Time: Monday, June 04, 2018 4:10:17 PM
Running script CreateProjectFolders...
Failed script Create Project Folders...
 Traceback (most recent call last):
 File "C:\temp\Pro_Folder_Structure\NEW_FOLDERS.py", line 10, in <module>
 os.makedirs(newDir)
 File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\os.py", line 220, in makedirs
 mkdir(name, mode)
PermissionError: [WinError 5] Access is denied: 'C:\\Program Files\\ArcGIS\\Pro//Data'
 Failed to execute (CreateProjectFolders).
Failed at Monday, June 04, 2018 4:10:17 PM (Elapsed Time: 0.06 seconds)

My assumption is that os.getcwd() would get the directory that the pro project is saved to. curr = dirpath = arcpy.env.workspace doesn't appear to do anything here. 

0 Kudos
12 Replies
ThomasColson
MVP Frequent Contributor

Blank project saved to default project path, with the tool box in the project folder

0 Kudos
DanPatterson_Retired
MVP Emeritus

Of simply put the results in 'tweet' so you can use it in within and without environments

import arcpy  # assuming you will be using arcpy anyway

 def tweet(msg):
    """Print a message for both arcpy and python.

    msg - a text message
    """
    m = "\n{}\n".format(msg)
    arcpy.AddMessage(m)
    print(m)
   

tweet(os.getcwd())  # tweet away

C:\GIS\A_Tools_scripts\Table_tools\Scripts
0 Kudos
DanPatterson_Retired
MVP Emeritus

If you want to have a parameter in your tool that sets the work dir

os.chdir(.....)  # set the work directory
0 Kudos