Directory listing function

This will get you close. Found this on the net and made modifications to the command line.
Original URL is: Yahooist Teil der Yahoo Markenfamilie.

Henri, take whatever changes you want.... and thanks again.

Bruce

"""
replace.py
"""

import re #for subn
import string #for replace
import glob #for trouver tous les fichiers d'un repertoire
import sys #for sys.argv, sys.exit(0)
import os.path #for isdir
import os #for listdir

···

-----Original Message-----
From: Wayne Koorts [mailto:waynek@hotpop.com]
Sent: Wednesday, July 30, 2003 7:23 AM
To: wxPython
Subject: [wxPython-users] Directory listing function

Hi,

Does anybody have a function that I can use that will return
a list of
the paths of _all_ files on a particular drive? (Windows)
Otherwise can
somebody give me some guidelines for creating one?

Regards,
Wayne

#############################################################################################
# Configuration section

#Global switches. Set these to 1 (on) or 0 (off)
#Use_command_line_parameters = 1
Use_regular_expressions = 0 # You must be aware of Python's regular expressions to use this
VeryVerbose = 0 # Print replaced lines in DOS box
Verbose = 0
VeryVerbose = 0
Search_subdirectories = 0
Search_only = 0
Make_backup = 0
Backup_suffix = ".bak"

#Global variables
Directory = "."

#TempFile = "c:\\temp\\tmp.tmp" #The temporary file
if os.name == 'posix':
  TempFile = '/tmp/tmp.tmp'
else:
  TempFile = "c:/temp/tmp.tmp"
  
FileFilter = ["*.*"] #The type of file to modify
String_to_replace = ''
Replacing_string = ''

# Here is another example, you can add as many extensions as you like.
# All matching files will all be modified
#FileFilter = ["*.htm","*.html"]

# End configuration section
##############################################################################################

def display_help():
  print 'Search and replace in multiple files and directories'
  print 'using plain text or regular expressions.'
  print ''
  print 'Usage: python replace.py [options] searchStr replaceStr'
  print ''
  print 'Options:'
  print ' -f ["fileSpec",] Specify file filter list. Default is ["*.*"]'
  print ' -d dir Specify search directory. Default is .'
  print ' -r Recurse subdirectories'
  print ' -re Use Python regular expressions'
  print ' -e Evaluate only'
  print ' -b Make backup file'
  print ' -be ext Specify backup file extension. Default is *.bak'
  print ' -t file Specify temp file. Default is tmp.tmp'
  print ' -v Verbose'
  print ' --verbose Very verbose'
  print ' -h Display this help'
# End display_help()

if len(sys.argv) < 3:
  display_help()
  sys.exit(0) #abandon

String_to_replace = sys.argv[-2]
Replacing_string = sys.argv[-1]

numArgs = len(sys.argv)

argNum = 1
while 1:
  if argNum >= numArgs - 2:
    break
  
  arg = sys.argv[argNum]
  
  if arg == '-f': argNum += 1; FileFilter = eval(sys.argv[argNum])
  elif arg == '-d': argNum += 1; Directory = sys.argv[argNum]
  elif arg == '-r': Search_subdirectories = 1
  elif arg == '-re': Use_regular_expressions = 1
  elif arg == '-e': Search_only = 1 # Evaluate only
  elif arg == '-b': Make_backup = 1
  elif arg == '-be': argNum += 1; Backup_suffix = sys.argv[argNum]
  elif arg == '-t': argNum += 1; TempFile = sys.argv[argNum]
  elif arg == '-v': Verbose = 1
  elif arg == '--verbose': VeryVerbose = 1
  elif arg == '-h': display_help()
  else:
    print 'Illegal option: ' + arg
    print ''
    display_help()
    
  argNum +=1
# End while

"""
if Use_command_line_parameters:
  if len(sys.argv) <> 5: # Tests the number of command line parameters
    print 'Usage: python replace.py dir orig_str replc_str file_filter_list'
    print 'Example: python replace.py Dir1 math path ["file.py"]'
    sys.exit(0) #abandon
  Directory = sys.argv[1]
  String_to_replace = sys.argv[2]
  Replacing_string = sys.argv[3]
  FileFilter = eval(sys.argv[4])
else: # up to you to change these strings
  Directory="."
  String_to_replace = "bad"
  Replacing_string = "worse"
"""

def replace_in(filename, str_to_replace, rep_string):
  try:
    ifh = open(filename,"r")
  except:
    print "cannot open",filename
    sys.exit(0)
  try:
    ofh = open(TempFile,"w")
  except:
    print "cannot open",TempFile
    sys.exit(0)
  nsub_total = 0
  while 1:
    line = ifh.readline()
    if not line:
      break
    else:
      if VeryVerbose:
        if string.count(line,str_to_replace):
          print line
      if Use_regular_expressions:
        newline,nsub=re.subn(str_to_replace, rep_string, line)
        nsub_total += nsub
      else:
        newline=string.replace(line,str_to_replace, rep_string)
        if line <> newline:
          nsub_total += string.count(line, str_to_replace)
      ofh.write(newline)
  
  ifh.close()
  ofh.close()
  if Search_only == 0:
    if nsub_total:
      if Make_backup:
        copy_file(filename, filename+Backup_suffix)# Do this first!!!
      copy_file(TempFile,filename)
  if Verbose or VeryVerbose:
    print nsub_total,"replacements done in",filename

def copy_file(from_file, to_file):
  try:
    ifh = open(from_file,"r")
  except:
    print "Cannot open",from_file
    sys.exit(1)
  try:
    ofh = open(to_file,"w")
  except:
    print "Cannot open",to_file
    sys.exit(2)
  while 1:
    line = ifh.readline()
    if not line:
      break
    else:
      ofh.write(line)
  ifh.close()
  ofh.close()

def search_and_replace(path):
  global String_to_replace, Replacing_string, Search_subdirectories
  for pattern in FileFilter:
    for filename in glob.glob(path+'\\'+pattern):
      if Verbose or VeryVerbose:
        print "replacing in",filename
      replace_in(filename,String_to_replace,Replacing_string)
  if Search_subdirectories:
    for entry in os.listdir(path):
      if os.path.isdir(path+"\\"+entry): # if it's a directory (could have been a file)
        search_and_replace(path+"\\"+entry)

# Go!
search_and_replace(Directory)
try:
  os.remove(TempFile)
except:
  pass