Comparing file extensions on the fly (warning: absolute noob posting)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • athos
    New Member
    • Mar 2008
    • 3

    Comparing file extensions on the fly (warning: absolute noob posting)

    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.

    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()
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    Not really looking over your code but just answering the simple question, how about just test for the existence of the file like this using the + operator to add the extra characters.

    I have two trash files on my system called /tmp/test.txt and /tmp/test.txt.xml, I am setting name explicitely but in your code it would come from the list
    Code:
    name='/tmp/test.txt'
    if os.path.isfile(name):
    	 if os.path.isfile(name+'.xml'):
    		 print open(name+'.xml','r').readlines()
    	 else: print 'no xml'
    else: print 'no file'
    ['a 1 2 3\n', 'b 3 4 5\n', 'c 5 6 7']
    
    name='/tmp/notest.txt'
    if os.path.isfile(name):
    	if os.path.isfile(name+'.xml'):
    		print open(name+'.xml','r').readlines()
    	else: print 'no xml'
    else: print 'no file'
    no file

    Comment

    • athos
      New Member
      • Mar 2008
      • 3

      #3
      Thanks so much for the response. Yeah, that's very close to what I had written, but it turns out I had put the snippet of code in the wrong place for it to work. I appreciate the help.

      Originally posted by dshimer
      Not really looking over your code but just answering the simple question, how about just test for the existence of the file like this using the + operator to add the extra characters.

      I have two trash files on my system called /tmp/test.txt and /tmp/test.txt.xml, I am setting name explicitely but in your code it would come from the list
      Code:
      name='/tmp/test.txt'
      if os.path.isfile(name):
      	 if os.path.isfile(name+'.xml'):
      		 print open(name+'.xml','r').readlines()
      	 else: print 'no xml'
      else: print 'no file'
      ['a 1 2 3\n', 'b 3 4 5\n', 'c 5 6 7']
      
      name='/tmp/notest.txt'
      if os.path.isfile(name):
      	if os.path.isfile(name+'.xml'):
      		print open(name+'.xml','r').readlines()
      	else: print 'no xml'
      else: print 'no file'
      no file

      Comment

      Working...