Skip to content

Instantly share code, notes, and snippets.

@juanje
Created March 29, 2012 00:12
Show Gist options
  • Save juanje/2231709 to your computer and use it in GitHub Desktop.
Save juanje/2231709 to your computer and use it in GitHub Desktop.
listdiff is just a silly script which merge two list and give you the elements which are not in the first list.
#!/usr/bin/python
# -*- coding: utf8 -*-
#
# Copyright (c) 2008-2012, Juanje Ojeda Croissier <jojeda@emergya.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# Authors : Juanje Ojeda Croissier <jojeda@emergya.com>
'''
listdiff is just a silly script which merge two list and give you the elements
which are not in the first list.
The script takes two parameters:
$ listdiff.py file1 file2
file1 and file2 must be paths of a real files in each one should be stored a
list of element to compare. Each element in a line.
A classic scenario where this is useful is the one where you have a command
who give you a list of things from a system and other (for example the list of
packages installed on two different machines) and you like to see which
elements do you have in the first, but not in the second one.
'''
__author__ = "Juanje Ojeda Croissier <jojeda@emergya.es>"
__copyright__ = "Copyright 2008-2012, Juanje Ojeda Croissier <jojeda@emergya.com>"
__credits__ = __author__
__license__ = "GPL-3"
__version__ = "0.1"
import sys
def file_to_list(filename):
"""file_to_list(filename) -> set
filename: file path with list of elements inside
Read all lines from a file and create a set (list) which will
store all the lines. Each one as one item of the list
Note: 'set' is a python type for a unordered collections of
unique elements
"""
aux_list = []
lines = open(filename).readlines()
for line in lines:
aux_list.append(line.strip())
return set(aux_list)
def usage():
#FIXME: Create this function
print "How to use it"
def error(msg):
"""Show error message and exit from the script
msg: This is the message to be showed
"""
print "Errot: %s" % msg
sys.exit(1)
if __name__ == '__main__':
if len(sys.argv) != 3:
error("Need exactly 2 arguments")
file_one = sys.argv[1]
file_two = sys.argv[2]
#FIXME: Check the files does exist in the filesystem
list_one = file_to_list(file_one)
list_two = file_to_list(file_two)
# Create a new list with the items are just in the first list
new_list = list_one - list_two
for item in new_list:
print item
# vim:ai:et:sts=4:tw=80:sw=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment