Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search and Sort Shapefiles
#1
Hello 
How can I search my shapefiles using python codes to find all shapefiles that start with for instance "landuse" and show the results in a message box? 
I used the following code but they return nothing. 
Would you have a look on them and remind me about errors?
Thanks for help.

# import arcpy module

import arcpy
import sys
from arcpy import env
import os
import fnmatch
#the list of variables all hard coded to decrease errors 
# Set the workspace for  ListFeatureClass functions
env.workspace = "C:/GIS_database/module5/module5data.mdb"
# set the variables
# path to the shapefiles
folder = "C:/GIS_database/module5/module5data.mdb"
# Looking for all files starting with landuse and a wildcard *
pattern = 'landuse*.shp'
# This is a master shapefile
filename = "C:/GIS_database/module5/land_use_merged.shp"
# This is the list of shapefiles starting landuse
shpList = []
# add file names to shpList.
for path,dirs,files in os.walk(folder):
   for filename in fnmatch.filter(files, pattern):
        shpList.append(os.path.join(path, filename))
# Add all names stored in shpList to a message box
for fc in shpList:
   arcpy.AddMessage(fc)
Reply
#2
(May-12-2017, 03:39 PM)babakkasraie Wrote: Hello 
How can I search my shapefiles using python codes to find all shapefiles that start with for instance "landuse" and show the results in a message box? 
I used the following code but they return nothing. 
Would you have a look on them and remind me about errors?
Thanks for help.

my code here
# import arcpy module

import arcpy
import sys
from arcpy import env
import os
import fnmatch
#the list of variables all hard coded to decrease errors 
# Set the workspace for  ListFeatureClass functions
env.workspace = "C:/GIS_database/module5/module5data.mdb"
# set the variables
# path to the shapefiles
folder = "C:/GIS_database/module5/module5data.mdb"
# Looking for all files starting with landuse and a wildcard *
pattern = 'landuse*.shp'
# This is a master shapefile
filename = "C:/GIS_database/module5/land_use_merged.shp"
# This is the list of shapefiles starting landuse
shpList = []
# add file names to shpList.
for path,dirs,files in os.walk(folder):
   for filename in fnmatch.filter(files, pattern):
        shpList.append(os.path.join(path, filename))
# Add all names stored in shpList to a message box
for fc in shpList:
   arcpy.AddMessage(fc)


You need to use ctrl + shift + v to paste code between python code blocks
Reply
#3
(May-12-2017, 03:39 PM)babakkasraie Wrote:
folder = "C:/GIS_database/module5/module5data.mdb"
for path,dirs,files in os.walk(folder):
An .mdb file is probably not a folder, so os.walk wouldn't return anything at all. Which is probably why you're not seeing any output at all :p
Reply
#4
Thanks 

I will try folder.

Thanks Nilamo 

Next time I will try the code tags. Well I still don't know how I should find and use them. 
A little instruction,, if possible for you, will be a good help, Thanks. 

Regards 
Babak
Reply
#5
See BBCode help
Reply
#6
I transferred the shapefiles into a folder and the script works now. 
Amazing help
Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sort search results by similarity of characters jacksfrustration 5 469 Feb-16-2024, 11:59 PM
Last Post: deanhystad
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,346 Apr-19-2022, 06:50 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020