Python Scrip for listing work space items erroring

690
9
05-29-2019 03:38 PM
DevinWelty1
New Contributor II

I am trying to run a script to list items out in a work space and I've used this script before except now I am getting errors.

My experience with python is limited.... I did not create the script, so I have no clue what is happening.

This is the error I keep receiving.

Traceback (most recent call last):
File "Source\Path", line 42, in <module>
out = open(output, 'w')

Here's a screen shot of the script. Any Ideas why it isn't working now?

Thank you

Tags (1)
0 Kudos
9 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Lines numbers would help... this is how

/blogs/dan_patterson/2016/08/14/script-formatting 

DevinWelty1
New Contributor II

Thank you for that! 

#!/usr/bin/env python
print('Running script')

print('Importing libraries')
import arcpy
import os

### enter paths here
source = r'E:' #r is necessary because of backslashes
output = 'E:\listdata2019.txt' #alternatively you can use double backslashes instead.
###

arcpy.env.workspace = source # the specific functions being used in this script require the environment to be set to input database.

### defines different functions for use in reading the workspace
#gets source features for copy
def listFeaturesInGDB():
    ''' set your arcpy.env.workspace to a gdb before calling '''
    for fds in arcpy.ListDatasets('','feature') + ['']:
        for fc in arcpy.ListFeatureClasses('','',fds):
            yield os.path.join(fds, fc)

#gets source tables for copy
def listTablesInGDB():
    ''' set your arcpy.env.workspace to a gdb before calling '''
    for tbl in arcpy.ListTables('',''):
        yield tbl

#gets source rasters for copy
def listRastersInGDB():
    ''' set your arcpy.env.workspace to a gdb before calling '''
    for rstr in arcpy.ListRasters('',''):
        yield rstr
        
print('Getting workspace items')
features = listFeaturesInGDB()
tables =  listTablesInGDB()
rasters = listRastersInGDB()

#writes output to file
print('Writing to output file' + '\n')
out = open(output, 'w')
out.write('###FEATURES###' + '\n')
for f in features:
    out.write(f + '\n') # the '\n' is a newline symbol
    print(f)
out.write('\n' + '###TABLES###' + '\n')
for t in tables:
    out.write(t + '\n')
    print(t)
out.write('\n' + '###RASTERS###' + '\n')
for r in rasters:
    out.write(r + '\n')
    print(r)

out.close()
print('\nScript finished')
0 Kudos
JoeBorgione
MVP Esteemed Contributor

in addition to what Dan suggests, is your output file really defined as

'C:\\pathtooutput\\output.txt'

or are you just using it as an example?

That should just about do it....
DevinWelty1
New Contributor II

I was just using that as the example, I'm trying to list data that is on an external drive (E:).

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

could be it is failing on reading the root folder of the external drive... try throwing them into a folder on that drive

LukeWebb
Occasional Contributor III

That Error Message only tells us the line it occurs, you seem to have left of the actual Error from the message. 

DevinWelty1
New Contributor II

I just ran it again to get the error and of course it worked.... . I do however want to list all of the data on the drive and not just what is within a .gdb...

So any suggestions how to edit this for that purpose?

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Not to steal Lukes thunder, but drive/directory/folder are all considered workspaces, so you can change create a list of workspaces, step through it, and list whatever is within it.  I've done, so I'll see if I can find the code for you...

This should get you going

arcpy.env.workspace = r'I:\GIS\ArcSDE\SuperUser' #obviously my drive


dirList = []
for w in arcpy.ListWorkspaces():
    dirList.append(w)

for d in dirList:
    arcpy.env.workspace = d
    print('Current workspace is {}'.format(d))
    for w in arcpy.ListWorkspaces():
         if 'sde' in w or 'gdb' in w:
             arcpy.env.workspace = w
             print('  Current gdb is {}'.format(w) )
             for t in arcpy.ListTables():
                 print('   {}'.format(t))
            #list tables shown above
            #list feature classes
            #list rasters
            # if you use feature datasets, you'd need to create a list
            # a list of them change your workspace and list features
         else:
            pass
That should just about do it....
0 Kudos
DevinWelty1
New Contributor II

Thank you

0 Kudos