Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys
- import os
- # Global variables
- file_names = []
- file_contents = []
- line = "" #temporary variable for line
- def help():
- print "USAGE: common_id FILE_1 FILE_2 ... FILE_N\n"
- return None
- def find_common(id1, id2):
- count = 0
- for i in xrange(len(file_contents[id1])):
- for ii in xrange(len(file_contents[id2])):
- if(file_contents[id1][i] == file_contents[id2][ii]):
- count += 1
- # print "id1 = %d, id2 = %d, count = %d" % (id1, id2, count)
- return count
- # Logics itself
- if (len(sys.argv) < 3 or sys.argv[1] == "-h"):
- help()
- exit()
- #Check filenames
- for i in xrange(1, len(sys.argv)):
- print "Filename = %s" % (sys.argv[i])
- if( os.path.exists(sys.argv[i]) and os.path.isfile(sys.argv[i]) ):
- file_names.append(sys.argv[i])
- else:
- print "ERROR: File %s not exists or is inaccessible!" % (sys.argv[i])
- print "Exiting..."
- exit()
- print "filenames len = %d" % (len(file_names))
- #Read files
- for f_id in xrange(len(file_names)):
- file_contents.append([])
- pfile = open(file_names[f_id], 'r')
- line = pfile.readline()
- # t_id = 1
- while (line != None) and (line != "" ):
- file_contents[f_id].append(line)
- line = pfile.readline()
- pfile.close()
- # print "%d" % (t_id)
- # t_id = t_id + 1
- # print "File = %s; len = %d" % ( file_names[f_id], len(file_contents[f_id]) )
- # print "Contents = %d" % (len(file_contents))
- # Another global variable
- out_table = [["" for x in xrange(len(file_names) + 1)] for x in xrange(len(file_names) + 1)]
- out_table[0][0] = "\\"
- out_str = ""
- tmp_int = 0
- tmp_str = ""
- for ix in xrange(len(file_names)):
- out_table[ix+1][0] = file_names[ix] # horizontal names
- out_table[0][ix+1] = file_names[ix] # vertical names
- for iy in xrange(len(file_names)):
- if out_table[ix+1][iy+1] == "" :
- if ix == iy:
- out_table[ix+1][iy+1] = "\\"
- else:
- out_table[ix+1][iy+1] = str(find_common(ix,iy))
- out_table[iy+1][ix+1] = out_table[ix+1][iy+1] # copying identical values
- for ix in xrange(len(out_table)):
- for iy in xrange(len(out_table[ix])):
- out_str += out_table[ix][iy]
- if( iy < (len(out_table) - 1)):
- out_str += ", "
- out_str += "\n"
- print out_str
- pfile = open('out.csv', 'w')
- pfile.write(out_str)
- pfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement