Find Files in Directory Using Python

July 10, 2008 at 5:53 pm 2 comments

A nice solution to this is the path Python module. However, the following simple solution will do the trick. It doesn’t support wildcards at this point, but that could easily be added with some regular expression code.

def getFilesMatchingPattern(directory, nonWildCardPattern):
  fileList=os.listdir(directory)
  return [f for f in fileList if f.find(nonWildCardPattern) > -1]

Advertisements

Entry filed under: CodeSnippet, Python.

How Many Files in a Linux Directory Increase the Text Size on Axes of Plots in R

2 Comments Add your own

  • 1. sahasranaman  |  July 21, 2008 at 9:57 am

    thanks for the tip.

  • 2. akismet-755a00ff9244038ec7162bb1847911c3  |  December 7, 2011 at 3:00 pm

    nice, thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed



%d bloggers like this: