Batch Re-projection script syntax error

4664
13
01-21-2015 10:32 AM
SalmanAhmed
New Contributor III

I have started learning Python for ArcGIS last week and have a simple script to reproject all the datasets in a folder. The script says "syntax error- invalid syntax" but I can't find anything. Can someone please have a look? It seems something very basic but I'm pretty new so I don't see it at the moment. Also running this in ArcGIS prints "failed" right away. And the files are in place in the right folders. I have already checked.

 

import arcpy

... sourceWorkspace = "C:\\Users\\tx8p90\\Desktop\\Lesson2"

... targetProjection = "C:\\Users\\tx8p90\\Desktop\\python excercise data\\Lesson1\\contourlines.shp"

... listDataSet = arcpy.ListDatasets(sourceWorkspace)

... try:

...     for x in listDataset:

...         outputDataset = sourceWorkspace + "\\projected_" + x

...         arcpy.Project_management(x, outputDataset, targetProjection)

...         print "Reprojection successful"

... except:

...     print arcpy.GetMessages()

...     print "failed"

0 Kudos
13 Replies
SalmanAhmed
New Contributor III

D:\Tmp

None

This is what I get when I run your code

0 Kudos
DarrenWiens2
MVP Honored Contributor

You have two different cases for your dataset list: listDataSet and listDataset. These are two different things in Python.

SalmanAhmed
New Contributor III

Thanks this was a valid error. But even after I correct it nothing changes. Still Pythonwin tells me invalid syntax and arcmap doesnt do anything after running the code.

0 Kudos
SalmanAhmed
New Contributor III

Okay so I finally figured this out. It was a stupid mistake on my part. The files I had in the specified folders were actually feature classes and not datasets. Anyway it is not possible to create a dataset inside a folder. It can only be done inside a geodatabase. So for someone who knows their way around with ArcGIS, they could have noticed that I had just a folder in the code but I was calling ListDataSets()function which is wrong. Using ListFeatureClasses() solved the issue and the files were properly reprojected. Thanks for all the inputs.