Windows command line not displaying print commands

JonathanB doulos05 at gmail.com
Tue Mar 31 01:00:04 CEST 2009


#This is pyFind, a python replacement for find(1)
import os, sys, re, fnmatch
from os.path import join
from optparse import OptionParser

usage = "usage: %prog --name <filename> [directory1 directory2]"
parser = OptionParser(usage=usage)
parser.add_option("--regex", dest="regex",
help="REGEX MATCHING DOES NOT WORK AT THIS TIME, ONLY USE THE --name
OPTION!!!")
parser.add_option("--name", dest="name",
help="Unix style pattern matching search for a file by filename.")


def set_directories(args, directories):
    """initialize directory list to ensure that all subsequent loops
will
    loop correctly. directories is a created list and args is the
positional
    arguments from OptionParser."""
    if args != []:
        for arg in args:
#We have to use os.splitdrive() to support Windows. Otherwise you
cannot search
#different drive letters.
            (drive, tail) = os.path.splitdrive(arg)
            directories.append([drive, tail])
#For handling the default case, which is to search in current working
directory
    else: directories.append(os.path.splitdrive(os.getcwd()))

def regex_matcher(value, names):
    if value == None:
        return names
    else:
        regex = re.compile(value)
        return [name for name in names if regex.search(name)]

def glob_matcher(value, names):
    if value == None:
        return names
    else: return [name for name in names if fnmatch.fnmatch(name,
value)]

if __name__ == "__main__":
    (options, args) = parser.parse_args()

    directories = []
    set_directories(args, directories)

    for directory in directories:
#If we are in windows, directory[0] will be True. In that case we must
switch
#to the root of that drive letter before we can traverse to the
requested
#directory.
        if directory[0] == True:
            os.chdir(directory[0])
#Now that we know we are either in the right drive letter or in a Unix
#environment, we can change to the proper directory.
        os.chdir(directory[1])
        for root, dirs, files in os.walk(os.getcwd()):
#            results = regex_matcher(options.regex, files)
#            regex = re.compile(options.regex)
#            results = [name for name in files if regex.search(name)]
            results = glob_matcher(options.name, files)
            if results != []: print"/n".join(results)



More information about the Python-list mailing list