[Tutor] Help needed with script to batch-create shapefiles
Brian van den Broek
bvande at po-box.mcgill.ca
Thu Feb 17 18:14:38 CET 2005
Chris Bromley said unto the world upon 2005-02-17 11:05:
> Prior to running the script I use the check button in the
> PythonWin and the scripts syntax is fine. When I run the script
> though, the message
> Script C:\ dBase_File_To_Shapefile.py returned exit code 0
> appears in the status bar at the bottom of the PythonWin window.
> The following text also appears in the Interactive window
> Traceback (most recent call last): File
> "C:\dBase_File_To_Shapefile2.py", line 37, in ?
> GP.FeatureClassToShapefile_conversion(outxyLayer, outputShapefiles)
> File "", line 2, in FeatureClassToShapefile_conversion com_error:
> (-2147467259, 'Unspecified error', None, None) Executing:
> FeatureClassToShapefile C:/xyLayerFiles/R302190.dbf C:\Shapefiles
> C:\Shapefiles Start Time: Thu Feb 17 14:23:09 2005 Running script
> FeatureClassToShapefile... Error in script FeatureClassToShapefile.
> Error in executing: cmd.exe /C
> "C:/xyLayerFiles/R302190.dbf" "C:\Shapefiles" "C:\Shapefiles"
> Failed to execute (FeatureClassToShapefile). End Time: Thu Feb 17
> 14:23:10 2005 (Elapsed Time: 1.00 secs)
> Another possibility is that all the slashes in the workspace paths
> in the code are forward slashes, whereas all paths appear with back
> slashes in the address bar in my computer. However, if I try
> changing the forward slashes to back slashes in the code I get a
> warning about syntax when I use the 'check' button.
As to what the difficulty is, I've no idea. But the forward slash,
backward slash thing isn't the problem.
Windows broke the previous standard of using '\' as an escape
character. In a string a '\' means "this next character is to be
treated as a special code.". Try:
So, to use backslashes in file paths, you either have to use raw
strings or double them up. '\\' gets interpreted as "the next
character is to be treated as special code -- no, wait, its a regular
Fortunately, Python is smart enough to let you use '/' for path
specifications, and it works out what's appropriate for your environment:
>>> # Look, ma, I tested *this* code
>>> a = open('C:/sample_file.txt', 'w')
>>> a.writelines(["I wrote to this file using /'s.\n",
"I read from it using \\'s.\n"])
>>> b = open('C:\\sample_file.txt', 'r')
>>> for line in b:
I wrote to this file using /'s.
I read from it using \'s.
More information about the Tutor