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

2563
7
02-08-2019 08:32 AM
ronancorrigan
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

thanks lads and lasses

Tags (1)
0 Kudos
7 Replies
DanPatterson_Retired
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

DanPatterson_Retired
MVP Esteemed Contributor

share this

/blogs/dan_patterson/2016/08/14/filenames-and-file-paths-in-python 

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

0 Kudos
JoshuaBixby
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'>
>>> 
DanPatterson_Retired
MVP Esteemed Contributor

we failed to add the multiplatform solution... /

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

ronancorrigan
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

0 Kudos
DanPatterson_Retired
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.

Did you read the link in my second post?

Filenames and file paths in Python 

 

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'
JoshuaBixby
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?

0 Kudos