maphew

Path strings

May 7, 2020

How to write paths line by line, for easy scanning and understanding, yet also easily turn them into lists for using?

Writing

pp = r"C:\Program Files\ArcGIS\Pro" 
PRO_PATHS = inspect.cleandoc(rf""" 
{pp}\bin 
{pp}\Resources\ArcPy 
{pp}\Resources\ArcToolbox\Scripts 
""") 
  • each path is on a separate line 
  • the common prefix is moved out of the way, for faster reading 
  • python indentation is respected for writing, but the result has indentation removed (this is what inspect.cleandoc() does). 

Using

Extend Windows PATH

winpaths = ';'.join(PRO_PATHS.splitlines()) 
os.environ['PATH'] = ';'.join((winpaths, os.environ['PATH'])) 
  • Formats to match Win PATH requirements by substituting semi-colon for line feeds 
  • inserts the new paths to beginning of PATH 

Path strings

install_dir = r"C:\Program Files\ArcGIS\Pro" 
dirs = ['bin', 'Resources\\ArcPy', 'Resources\\ArcToolbox\\Scripts'] 

for d in dirs: 
	os.environ['PATH'] = ';'.join(( 
	os.path.join(install_dir, d), 
	os.environ['PATH'])) 

I had a really hard time following what was going in this code. Granted the new winpaths = … is also opaque, but I don't care because that's not the part I'll need to look at and update in future.