Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Finding a files based on search pattern

Posted on 2008-10-01
1
Medium Priority
?
676 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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 …

618 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