#! /usr/bin/python """ #+ # NAME: # fcdir # PROCEDURE: # Compare ascii files # CALLING SEQUENCE: # fcdir.py dir_fc dir_base # INPUTS: # dir # dir_base # CALLS: # PROCEDURE: # > Implemented as a python script # > Files in directory 'dir' are compared with files # in directory 'dir_base' having the same base name # (file name, plus extension). # MODIFICATION HISTORY: # MAY-2002, Paul Hick (UCSD/CASS) # FEB-2004, Paul Hick (UCSD/CASS; pphick@ucsd.edu) # Added delete keyword; changed 'r' to 'br' in open # statements to allow for processing of binary files. #- """ import sys, os from tiny import args, is_there if __name__ == '__main__': win32 = sys.platform == 'win32' arg = args( sys.argv ) delete = is_there( '-delete', sys.argv ) narg = len(arg) if narg == 3: dir = arg[1] base_dir = arg[2] else: dir = raw_input('Directory : ') base_dir = raw_input('Comparison directory : ') if os.path.isdir(dir): files = os.listdir(dir) else: print 'Not a directory: ', dir tmp = raw_input('end') sys.exit(1) if os.path.isdir(base_dir): base_files = os.listdir(base_dir) else: print 'Not a directory: ', base_dir tmp = raw_input('end') sys.exit(1) names = [] for file in files: names.append( os.path.basename(file) ) base_names = [] for base_file in base_files: base_names.append( os.path.basename(base_file) ) for iname in range( len(names) ): file = files[iname] file = os.path.join(dir,file) if os.path.isfile(file): name = names[iname] if base_names.count(name) == 1: ibase = base_names.index(name) base_file = base_files[ibase] base_file = os.path.join(base_dir,base_file) if win32: same_file = file == base_file else: same_file = os.path.samefile(file, base_file) if same_file: print 'Same file:' print ' >> ',file print ' >> ',base_file else: lines = (open(file , 'br')).read() base_lines = (open(base_file, 'br')).read() length = len(lines) base_length = len(base_lines) print ' >> ',file , ' (',length ,')' print ' >> ',base_file, ' (',base_length,')' if length != base_length: print 'Files have different sizes' elif lines == base_lines: print 'Files are identical' if delete: print 'rm', file os.remove( file ) else: print 'Files have same length, but are not identical' else: print file, 'Count: ', base_names.count(name) else: print 'Not a file: ',file tmp = raw_input('end') tmp = raw_input('end') sys.exit(1)