Python Glob.Glob

594
4
Jump to solution
04-12-2021 06:24 PM
Vdawg1990
New Contributor

Hi,

Can someone please shed some light on why my glob.glob statement is not working?

It works on shapefiles just not feature classes in geodatabases.

I am simply just trying to put in a file path to a feature class in the geodatabase. The file path however needs to include a wild card (*) as the date/name of the folder can change each month.

I have tried the below statement but i get the error below

TC = glob.glob("L:/Support/Data_load/Tenements/%s/SA/Kennels/Popcorns*/Popcorn.gdb/Popcorn123" % (d))[0

IndexError: list index out of range

 

Does glob.glob not support geodatabase? and if so is there another solution?

 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

A wildcard used in my examples returns a list of featureclasses like glob.glob.

If you are using the first one, then you would slice as per usual ( [0] ).  If there was only one, then you still slice the list.

Or you have a list to cycle through if you are working with many featureclasses.

If you examined the contents of a *.gdb "folder", you will see there is no usable naming convention except through the ListFeatureClasses approach


... sort of retired...

View solution in original post

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

List all featureclasses in a gdb.

Then list only those that begin with a "b"

 

import arcpy
gdb = r"C:\arcpro_npg\npg\Project_npg\tests.gdb"
arcpy.env.workspace = gdb
fcs = arcpy.ListFeatureClasses("*")

fcs = arcpy.ListFeatureClasses("b*")

fcs = arcpy.ListFeatureClasses("*")

b_fcs = arcpy.ListFeatureClasses("b*")

fcs
['big', 'odd', 'odd_inters', 'odd_dis', 'odd_sub', 'odd5', 'odd5_centr', 'odd5_inside', 'odd_vert', 'sq', 'sq_v', 'Voronoi', 'Voronoi2', 'x2', 'multi', 'sq2', 'shps', 'v0', 'c', 'sq2_mabr', 'sq2_delaunay', 'c_pnts', 'sq_MeshPnts', 'sq2_vert', 'c_ln', 'c_segs', 'concave_c', 'c_dens', 'sq_diss', 'hex_flat', 'rectangles', 'triangles', 'hex_shift', 'dissolve', 'dissolve1', 'aoi', 'rot', 'CC', 'c0', 'c1', 'CCply', 'c0ply', 'b0b1', 'b0_b1', 'b0_b1_ints', 'b0', 'b0_ints']

b_fcs
['big', 'b0b1', 'b0_b1', 'b0_b1_ints', 'b0', 'b0_ints']

 

BTW

glob.glob wouldn't be able to decipher any useful filenames and/or extensions.  Examine the contents of a *.gdb using windows File Explorer and you will see what I mean

 


... sort of retired...
0 Kudos
Vdawg1990
New Contributor

Hi Dan,

Thank you for your response.

How do i use the feature class from the list as a variable though?

i.e How would i use the featureclass 'big' as variable that i can put in other commands?  (Like arcpy.addfield)

Also i apologise i was not clear, the wild card is not in the featureclass name but in the folder name. (what you have written is still beneficial as at some stage i will need wild cards for my feature classes)

TC = glob.glob("L:/Support/Data_load/Tenements/%s/SA/Kennels/Popcorns*/Popcorn.gdb" % (d))[0

I am wanting to use my feature class as a variable that i can use in addfield and other commands, but the wildcard in the folder path is causing errors"

D1 = arcpy.AddField_management(Popcorn123, "Ext_Date", "DATE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

A wildcard used in my examples returns a list of featureclasses like glob.glob.

If you are using the first one, then you would slice as per usual ( [0] ).  If there was only one, then you still slice the list.

Or you have a list to cycle through if you are working with many featureclasses.

If you examined the contents of a *.gdb "folder", you will see there is no usable naming convention except through the ListFeatureClasses approach


... sort of retired...
0 Kudos
Vdawg1990
New Contributor

Hi Dan,

 

Ah understood!

Yes in my case there is only one. I have tried the index and it works like a charm :) Thank you

D1 = arcpy.AddField_management(b_fcs[0], "test999", "DATE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

 

0 Kudos