OS: Windows 2000/XP (needs to run on 2 different machines)
Language: Python 2.5
Programmer Level: Pathetically new to Python
Goal: Using code I've altered for my needs, I'm attempting to create a drive crawler that will search for certain files (*.svx;*.mdb) and on the fly and check to see if there is a corresponding .xml file. (e.g. Find all "*.svx". If filename.svx, then is there a filename.svx.xm l?). If there is a corresponding file, then print to screen, if not, then print original filename.svx to an error log
I've seen code for splitting and finding a similar filename with a different extension, but what I'm doing here is really adding another extension on to the original. I'm sure I need to use a similar process, and assign a new variable to represent the .xml files, but my attempts so far have resulted in either producing lists of all .xmls on the drive or various error codes. Can anyone help me? Much Thanks.
Language: Python 2.5
Programmer Level: Pathetically new to Python
Goal: Using code I've altered for my needs, I'm attempting to create a drive crawler that will search for certain files (*.svx;*.mdb) and on the fly and check to see if there is a corresponding .xml file. (e.g. Find all "*.svx". If filename.svx, then is there a filename.svx.xm l?). If there is a corresponding file, then print to screen, if not, then print original filename.svx to an error log
I've seen code for splitting and finding a similar filename with a different extension, but what I'm doing here is really adding another extension on to the original. I'm sure I need to use a similar process, and assign a new variable to represent the .xml files, but my attempts so far have resulted in either producing lists of all .xmls on the drive or various error codes. Can anyone help me? Much Thanks.
Code:
def Walk( root, recurse=0, pattern='*.svx;*.mdb', return_folders=0 ):
import fnmatch, os, string
# initialize
result = []
# must have at least root folder
try:
names = os.listdir(root)
except os.error:
return result
# expand pattern
pattern = pattern or '*'
pat_list = string.splitfields( pattern , ';' )
# check each file
for name in names:
fullname = os.path.normpath(os.path.join(root, name))
# grab if it matches our pattern and entry type
for pat in pat_list:
if fnmatch.fnmatch(name, pat):
if os.path.isfile(fullname) or (return_folders and os.path.isdir(fullname)):
result.append(fullname)
continue
# recursively scan other folders, appending results
if recurse:
if os.path.isdir(fullname) and not os.path.islink(fullname):
result = result + Walk( fullname, recurse, pattern, return_folders )
return result
qcinputlist = 'qc.tmp'
qcinputlist = open('qc.tmp', 'w')
if __name__ == '__main__':
# test code
files = Walk('.', 1, '*.svx;*.mdb', 1)
print 'There are %s files below current location:' % len(files)
# write output to file to be read in by subsequent process
print 'Nearly done...'
for file in files:
qcinputlist.write(file+ '\n')
qcinputlist.close()
Comment