Solved

Finding a files based on search pattern

Posted on 2008-10-01
1
666 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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 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 …

746 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

10 Experts available now in Live!

Get 1:1 Help Now