Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm very new to python and have created a script for enabling a user to list file types in a workspace making use of the parameters function in ArcGIS so it's more user friendly. However I cannot get the script to work...I am struggling to finish the script off can anyone lend a hand?

Python reports the syntax error in line 36 "f.close()" there is no white space around it so am struggling to see whats wrong...can you? Or what am I missing?

Here's the code:

import arcpy
from arcpy import env
import os
env.workspace = ("C:\ArcGIS\Default.gdb")
ws = arcpy.GetParameter(0)
option = arcpy.GetParameterAsText(1)
outputFilePath = arcpy.GetParameterAsText(2)

dsList = []

if option == "List Feature Classes":
    featureclasses = arcpy.ListFeatureClasses(ws)
    for table in tables:
        dsList.append(table)
elif option == "List Tables":
    tables = arcpy.ListTables(ws)
    for table in tables:
        dsList.append(table)
elif option == "List Rasters":
    rasters = arcpy.ListRasters(ws)
    for table in tables:
        dsList.append(table)
elif option == "List Workspaces":
    workspaces = arcpy.ListWorkspaces(ws)
    for table in tables:
        dsList.append(table)
elif option == "List Files":
    files = arcpy.ListFiles(ws)
    for table in tables:
        dsList.append(table)

f = open(outputFilePath, 'w') 

for item in dsList:

f.close()
share|improve this question

2 Answers 2

You don't have a body in your for loop:

f = open(outputFilePath, 'w') 

for item in dsList: << This is a empty for block
   # need something here

f.close()

I think you will also get error when you run it because tables isn't defined in a lot of cases

if option == "List Feature Classes":
    featureclasses = arcpy.ListFeatureClasses(ws)
    for table in tables: << where is tables defined
        dsList.append(table)
elif option == "List Tables":
    tables = arcpy.ListTables(ws)
    for table in tables:
        dsList.append(table)
elif option == "List Rasters":
    rasters = arcpy.ListRasters(ws)
    for table in tables: << where is tables defined
        dsList.append(table)
elif option == "List Workspaces":
    workspaces = arcpy.ListWorkspaces(ws)
    for table in tables: << where is tables defined
        dsList.append(table)
elif option == "List Files":
    files = arcpy.ListFiles(ws)
    for table in tables: << where is tables defined
        dsList.append(table)
share|improve this answer
    
Would {print to (outputFilePath)} work for printing to results to the defined outputFilePath? –  PeterPan Jul 19 '12 at 12:38
    
Ah yes...tables isn't defined –  PeterPan Jul 19 '12 at 12:40

Did you try to write something to this file:

for item in dsList:
    f.write(item + "\n")
f.close()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.