Solved

Finding a files based on search pattern

Posted on 2008-10-01
1
667 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
1 Comment
 
LVL 14

Accepted Solution

by:
RichieHindle earned 150 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python: how to print and format rows from API results 4 63
INSERT for sqlite3 in Python 9 81
Installing Flask app on IIS7 10 297
Macports Import beautifulsoup4 Problem 6 67
"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 …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now