Using r in workspace declaration

483
2
Jump to solution
09-26-2019 05:14 PM
SoratoSouza_e_Silva
Occasional Contributor II

Hi,

Why and When I need use r before the workspace declaration in python like below:

  1. import arcpy  
  2. arcpy.env.workspace = r"pathto\scratch.gdb"  

Thank´s

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

Search "python raw encoding" for the history.

Or read my precise

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

In short... the options are listed but a little 'r' goes a long way.

# ---- the bad

folder = "c:\users\andre souza\tools\gis"

  File "<ipython-input-35-6d2d90c647fc>", line 1
    folder = "c:\users\andre souza\tools\gis"
            ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape

# ---- the good

folder = r"c:\users\andre souza\tools\gis"

folder
'c:\\users\\andre souza\\tools\\gis'

# ---- the ugly

folder = "C:\Temp\andre souza\tools\gis"

print(folder)
C:\Tempndre souza      ools\gis

folder = r"C:\Temp\andre souza\tools\gis"

print(folder)
C:\Temp\andre souza\tools\gis

View solution in original post

2 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Search "python raw encoding" for the history.

Or read my precise

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

In short... the options are listed but a little 'r' goes a long way.

# ---- the bad

folder = "c:\users\andre souza\tools\gis"

  File "<ipython-input-35-6d2d90c647fc>", line 1
    folder = "c:\users\andre souza\tools\gis"
            ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape

# ---- the good

folder = r"c:\users\andre souza\tools\gis"

folder
'c:\\users\\andre souza\\tools\\gis'

# ---- the ugly

folder = "C:\Temp\andre souza\tools\gis"

print(folder)
C:\Tempndre souza      ools\gis

folder = r"C:\Temp\andre souza\tools\gis"

print(folder)
C:\Temp\andre souza\tools\gis
SoratoSouza_e_Silva
Occasional Contributor II

Fantastic! Help me a lot!!

Thank´s Dan!!

0 Kudos