Wednesday, September 19, 2007

Python basics: finding files

Here's a little snippet showing two ways to search for files. It highlights a potential issue when creating cross-platform code.

#!/usr/bin/env python
import os,fnmatch

def getfiles(path,filename):
"""Recursively search a path and generate a list of files found

Takes a filesystem path and filename as an argument. The path is
recursively searched for filename. Returns a list of each file found
(in absolute path format) with the first element of the list set to

Taken from Python tutor list.
filelist = ['start']
for root, dirs, files in os.walk(path):
for name in files:
a = os.path.join(root, name)
if os.path.isfile(a) and fnmatch.fnmatch(a, filename):
return filelist

def filefind(path,filename):
""" Using posix find to accomplish a similar filename search

This was done on a Mac... Note the need to include -H to follow symbolic
links on the Mac version. Not present in the Linux version. Points out why
the above is superior, especially given that find doesn't exist in the same
way on the PC.
fp = os.popen('find -H %s -name %s' % (path, filename))
print 'Executing find -H %s -name %s' % (path, filename)
filelist = fp.readlines()
for i in range(len(filelist)):
# chop off trailing newline
filelist[i] = filelist[i][:-1]
return filelist

if __name__ == "__main__":
filelist = filefind('/tmp','\*\.\*')
print "Filelist: ", filelist
filelist = getfiles('/tmp','*.*')
# Done to get rid of the 'start' that is the first element of the list
print "Getfiles: ", filelist[1:]

No comments: