how can i work around path names in arcpy having spaces in them, if i can?

2563
7
02-08-2019 08:32 AM
New Contributor

I'm trying to create an arcpy tool for one of the teams, and I have everything working. But when I redirect the path names to be where their data is saved and where they want the outputs to go to, I get errors like ERROR:00732 or ERROR:00210 and its because of the folder directories having spaces in their names. is there a work around for this? as getting the team to rename all their folders to exclude spaces isn't an easy sell

Tags (1)
7 Replies
MVP Esteemed Contributor
pth = "c:\the path\with\a\space\in\its\name"

print(pth)
c:      he path\with\space\in\its
ame

pth = r"c:\the path\with\a\space\in\its\name"

print(pth)
c:\the path\with\a\space\in\its\name‍‍‍‍‍‍‍‍‍‍

it isn't just spaces, any of a number of letters that follow the \ cause problems...

I have identified a few.

note the little 'r' before the path name?  that is called 'raw' encoding.

it is one of just many options.

since they won't get the smarts you will have to ensure that you do it for them

MVP Esteemed Contributor

even Microsoft doesn't recommend spaces and other flotsam in filenames or paths.

I bet the team has a space or a period in their 'User' profile too.  Caused us headaches for a while until IT and us got things straightened out

MVP Esteemed Contributor

I agree with Dan that spaces, commas, period, etc... in path names isn't a great idea, it raises the risk that some program will get tripped up when working with files and folders.  That said, many of those characters are not forbidden in ArcGIS paths.  If you start working with Python's os.path module and using raw strings, you should be fine:

>>> import arcpy
>>> path = r"C:\tmp\folder with spaces\user.me space"
>>> path
'C:\\tmp\\folder with spaces\\user.me space'
>>> arcpy.Exists(path)
True
>>> res = arcpy.CreateFileGDB_management(path, "fgdb")
>>> arcpy.CreateTable_management(res, "tbl")
<Result 'C:\\tmp\\folder with spaces\\user.me space\\fgdb.gdb\\tbl'>
>>> ‍‍‍‍‍‍‍‍‍‍
MVP Esteemed Contributor

we failed to add the multiplatform solution... /

path = "C:/tmp/folder with spaces/user.me space"

New Contributor

I have been using double quotes the whole time but I still get an error

below is an example fo what i'm doing;

arcpy.CopyRows_management(excelscript, r"{}\Testsheet.csv".format(location))
pfc_temp = r"{}\Testsheet.csv".format(location)

where location is a folder parameter. if I just set location to the folder in question directly it still comes up with the same error

MVP Esteemed Contributor

Ronan, it isn't just double quotes.  It has to be 'raw' string encoded... ie with the 'r' in front.

The big problem right now, I suspect is that the path string gets corrupted from the get-go.

any folder that begins with a variety of characters is going to be messed up unless it is raw encoded.. or avoided by fixing your folders or using Herculean effort to correct them.

And to show that it works... change your folder for testing and/or to move you along

location = "C:\Junk"
r"{}\Testsheet.csv".format(location)

'C:\\Junk\\Testsheet.csv'

location = "C:\Junk and space"
r"{}\Testsheet.csv".format(location)

'C:\\Junk and space\\Testsheet.csv'‍‍‍‍‍‍‍‍‍
MVP Esteemed Contributor

I agree with Dan's comments.  Additionally, it helps to paste the exact error and traceback.  I am also suspect of your location variable, what is stored in it?