Solved

Finding a files based on search pattern

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
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 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 …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

707 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