Select to view content in your preferred language

Python Novice Question - Get a list of files in a directory with particular extension

2067
15
08-20-2010 07:32 AM
SheriNorton
Frequent Contributor
I need to generate a list of all files in a directory with a *.MDB extension (personal geodatabases) in a Python script. Should I first split the path and filenames:

dirname = "C:\Arcwork"
(filepath, filename) = os.path.split(dirname)

and then somehow split the filename into name and extension (how?!), or vice versa?
0 Kudos
15 Replies
ChrisMathers
Deactivated User
I would:
dirlist=os.listdir(r"C:\Arcwork")
mdblist=[]
for item in dirlist:
    if item[-3:] in ['MDB','mdb']
        mdblist.append(item)


its kind of wordy and a bit brute-force but it works 😛
0 Kudos
SheriNorton
Frequent Contributor
Thanks - tried this but validation returns a syntax error.
0 Kudos
BBicking1
Esri Contributor
Thanks - tried this but validation returns a syntax error.


Hi...

MBldr's Iterate Files [ArcGIS10] with File Extension set to .mdb will do what you want. Connect the Collect Values ModelOnly Tool to the iterator's output to get the list. Note though at this time the list is not export-able or save-able.

Barbara Bicking
GeoprocessingTeam
0 Kudos
SheriNorton
Frequent Contributor
I've tweaked the code:

import os

dirlist=os.listdir("c:\\Arcwork")
mdblist=[]
for item in dirlist:
    if item.endswith("mdb"):
        print item
        mdblist.append[item]

print mdblist
_____________________________
But I get this error: do I need to import another module to use the "Append'?
Traceback (most recent call last):
  File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Arcwork\GetMasterMDBList.py", line 8, in <module>
    mdblist.append[item]
TypeError: 'builtin_function_or_method' object is unsubscriptable
0 Kudos
DanPatterson_Retired
MVP Emeritus
append uses round brackets
>>> a_list = []
>>> for i in [1,2,3,4]:
...  a_list.append(i)
...  
>>> print a_list
[1, 2, 3, 4]
>>> 
0 Kudos
KarlOrmer
Emerging Contributor
You want to call a method. You call objects by using round brackets.

clm42's snippet misses a `:` at the end of line 4.

Please note that Python is case-sensitive. Your attempt will fail if a file is - for whatever reason - called .MDB, .mDb or .mDB.
>>> a = 'test.MDB'
>>> a.endswith('mdb')
False

Therefore you need to "convert" your item variable to lower-case before testing for 'mdb'
>>> a = 'test.MDB'
>>> a.lower()
'test.mdb'
>>> a.lower().endswith('mdb')
True

This adds up to:
dirlist=os.listdir("c:\\Arcwork")
mdblist=[]
for item in dirlist:
    if item.lower().endswith("mdb"):
        print item
        mdblist.append(item)

print mdblist
0 Kudos
SheriNorton
Frequent Contributor
Presto!!! Thanks so much to you all. :o) I promise to be on the "giving" side down the road... Have a terrific weekend.

-Sheri
0 Kudos
Luke_Pinner
MVP Regular Contributor
Better late than never 🙂
import glob
for mdb in glob.glob('c:\\Arcwork\\*.mdb'):
    print mdb


The above snippet will return *.mdb and *.MDB (and *.MdB etc.) on case-insensitive filesystems, such as Windows.
For maximum portability, use something like:
for mdb in glob.glob(os.path.join(searchdir,*.[mM][dD][bB]')):
    print mdb
0 Kudos
SheriNorton
Frequent Contributor
Neat - thanks Luke! Sounds like this should work for Windows as well as Unix platform? I'll give a shot this morning. It's good to see there's usually more than one way of "skinning the cat".
0 Kudos