Select to view content in your preferred language

Wildcards?

1455
6
05-23-2011 06:53 AM
PaulBoehnlein
Emerging Contributor
I have a group of raster datasets, some .Tiff's, some .jpg's.  I want to perform arcpy.ImportMetadata_conversion on them.  I recall doing something similar in Windows command line using something like...

FOR %f in (*.tif) DO arcpy.ImportMetadata_conversion(example.xml, *.tif)

I think that's called a wildcard?  Is there something equivalent I can do in Python?  Could someone show me an example?  Please forgive my newbieness.
Tags (2)
0 Kudos
6 Replies
JasonScheirer
Esri Alum
You can use glob.

import arcpy
import glob
import os

directory = r"c:\tiffs\"

for tif in glob.glob(os.path.join("*.tif")):
    arcpy.ImportMetadata_conversion("example.xml", tif)
0 Kudos
PaulBoehnlein
Emerging Contributor
Jason,

Thanks for the reply. Your directory path has a slash at the end; that gives me a syntax error in IDLE. When I remove the slash (see below), the script runs, but the metadata file does not get imported. Any thoughts?

import os, sys, traceback, string, arcpy, glob

try:

    directory = r"C:\Rectified Inundation Maps"
    
    for tif in glob.glob(os.path.join("*.tif")):
        arcpy.ImportMetadata_conversion(r"C:\Rectified Inundation Maps\contact_PMI_maps.xml", tif)

    print "Import finished."
0 Kudos
JasonScheirer
Esri Alum
Oh, whoops. needs to be

directory = r"c:\tiffs\\"
0 Kudos
PaulBoehnlein
Emerging Contributor
That corrects the syntax error, but it still doesn't work. Any other suggestions?
0 Kudos
LoganPugh
Frequent Contributor
I think maybe Jason just forgot to use the directory variable in the os.path.join function. And you do not need trailing slashes when using os.path.join. This should work:
import arcpy
import glob
import os

directory = r"c:\tiffs"

for tif in glob.glob(os.path.join(directory, "*.tif")):
    arcpy.ImportMetadata_conversion("example.xml", tif)


If you need a recursive function (goes into all subdirectories of the initial directory), take a look at some of these examples: http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python
0 Kudos
PaulBoehnlein
Emerging Contributor
Thanks so much Logan.
0 Kudos