?
Solved

Finding a files based on search pattern

Posted on 2008-10-01
1
Medium Priority
?
673 Views
Last Modified: 2012-05-05
Hi,

Within a python script I'd like to find files based on an inputted search pattern (e.g test.py 'test*.txt') and place the files into an array, for subsequent processing.

Any thoughts on how to do this?

0
Comment
Question by:pixitron
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 14

Accepted Solution

by:
RichieHindle earned 600 total points
ID: 22616167
Here you go:
import sys, glob
 
# Collect the matching filenames here:
filenames = []
 
# Read one or more search patterns from the command line
for pattern in sys.argv[1:]:
    # For each pattern, expand it into a list of filenames.
    matches = glob.glob(pattern)
 
    # Add these matches to the list of all the matching files.
    filenames.extend(matches)
 
# We now have our list of filenames.
print filenames
 
# Example usage and output:
# > cd \temp
# > python ee-glob.py *.java test*
# ['Function.java', 'unescape.java', 'test.mbox', 'test.xls', 'TestApp.exe']

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question