Setting paths to data in Python
Geoprocessing tools and arcpy functions accept both strings and Pathlib.Path objects for paths.
Programming languages, such as Python, treat a backslash (\) as an escape character in strings. For instance, \n represents a line feed, and \t represents a tab. When specifying a path, a forward slash (/) can be used in place of a backslash. Two backslashes can be used instead of one to avoid a syntax error. A string literal can also be used by placing the letter r before a string containing a backslash so it is interpreted correctly.
import arcpy
arcpy.management.GetCount("c:/temp/streams.shp")
arcpy.management.GetCount("c:\\temp\\streams.shp")
arcpy.management.GetCount(r"c:\temp\streams.shp")
In the following sample, backslashes are used by mistake, and \t is interpreted as a tab by Python. The tool will fail, as the path is interpreted differently than it was intended.
import arcpy
arcpy.management.GetCount("c:\temp\streams.shp")
# ExecuteError: Failed to execute. Parameters are not valid.
# ERROR 000732: Input Rows: Dataset c: em\streams.shp does not exist or is not supported
# Failed to execute (GetCount)
Tip:
It is possible to have a feature class and a feature dataset with the same name contained in a geodatabase. In such a case, the feature class and feature dataset will have the same catalog path. Most tools work with one or the other. However, for those tools that can work with either, such as the Copy tool, the data type can be specified to avoid ambiguity.
Pathlib.Path
Using Pathlib.Path paths can help your script have better cross-platform compatibility and provides the benefits of working with object-oriented paths over strings.
import arcpy
import pathlib
shp_file = pathlib.Path(r"c:\temp\streams.shp")
arcpy.management.GetCount(shp_file)