Wickers
2005-11-15, 18:25
Hello all,
In light of the new forum.. I post a Python thread about directory content differences and sysadmin based work.
Currently this script is used on the CLI and returns both common and unique directory contents (by name)... it could be used by a sysadmin to check for differences in backup directories, but it's not quite there yet. I'd like to put in a check for the date of last modification, and size... Also, might want to make it recursive to dive into subdirectories if the subdirectory is common.
Anyhow, just sharing... if you have a chance, I've confirmed this works under "Linux > bash > python2.4" and "WinXP > command > python2.4", but I'd be interested to know if it fails on other platforms. (not that it should)
And if you are a python freak like myself, and you see something that needs to be changed or maybe my coding practices aren't kosher... give a shout.
No this is not a class project, so don't worry about doing 'my' homework... I've been learning python on my own, with the help of a friend.
"""dirdiff.py: List common and uncommon files and subdirectories between
two given directories.
Usage: #python dirdiff.py [dir...] [dir...]
v0.0.1b --AppleNova edition--
Copyleft 2005 ;) use as you will...
"""
import sys
import os
#CLI interface#
if sys.argv[1] in ('-h', '--help', '/?'):
print __doc__
sys.exit(0)
if len(sys.argv)>3:
print "Input not understood, use '-h' for help."
sys.exit(0)
#Take care of full length directory names if not given#
if sys.argv[1].startswith('/'):
dirA = os.listdir(sys.argv[1])
else:
dirA = os.listdir(os.getcwd() + "//" + sys.argv[1])
if sys.argv[2].startswith('/'):
dirB = os.listdir(sys.argv[2])
else:
dirB = os.listdir(os.getcwd() + "//" + sys.argv[2])
#Define the comparing code#
def compareSame(listA, listB):
same = []
count = len(listA)
while count > 0:
if (listA[count-1] in listB) == True:
same.append(listA[count-1])
count = count -1
return same
def compareDiff(listA, listB):
diff = []
count = len(listA)
while count > 0:
if (listA[count-1] in listB) == False:
diff.append(listA[count-1])
count = count -1
return diff
#Prep for report#
same = compareSame(dirA, dirB)
diffA = compareDiff(dirA, dirB)
diffB = compareDiff(dirB, dirA)
#Print report#
print "#########--SAME in both directories--#########"
for i in same:
print "%s: is a common file" % (i,)
print '#########--Unique in "'+sys.argv[1]+'"--#########'
for i in diffA:
print "%s: is a unique file in directory %s" % (i, ('"'+sys.argv[1]+'"'))
print '#########--Unique in "'+sys.argv[2]+'"--#########'
for i in diffB:
print "%s: is a unique file in directory %s" % (i, ('"'+sys.argv[2]+'"'))
In light of the new forum.. I post a Python thread about directory content differences and sysadmin based work.
Currently this script is used on the CLI and returns both common and unique directory contents (by name)... it could be used by a sysadmin to check for differences in backup directories, but it's not quite there yet. I'd like to put in a check for the date of last modification, and size... Also, might want to make it recursive to dive into subdirectories if the subdirectory is common.
Anyhow, just sharing... if you have a chance, I've confirmed this works under "Linux > bash > python2.4" and "WinXP > command > python2.4", but I'd be interested to know if it fails on other platforms. (not that it should)
And if you are a python freak like myself, and you see something that needs to be changed or maybe my coding practices aren't kosher... give a shout.
No this is not a class project, so don't worry about doing 'my' homework... I've been learning python on my own, with the help of a friend.
"""dirdiff.py: List common and uncommon files and subdirectories between
two given directories.
Usage: #python dirdiff.py [dir...] [dir...]
v0.0.1b --AppleNova edition--
Copyleft 2005 ;) use as you will...
"""
import sys
import os
#CLI interface#
if sys.argv[1] in ('-h', '--help', '/?'):
print __doc__
sys.exit(0)
if len(sys.argv)>3:
print "Input not understood, use '-h' for help."
sys.exit(0)
#Take care of full length directory names if not given#
if sys.argv[1].startswith('/'):
dirA = os.listdir(sys.argv[1])
else:
dirA = os.listdir(os.getcwd() + "//" + sys.argv[1])
if sys.argv[2].startswith('/'):
dirB = os.listdir(sys.argv[2])
else:
dirB = os.listdir(os.getcwd() + "//" + sys.argv[2])
#Define the comparing code#
def compareSame(listA, listB):
same = []
count = len(listA)
while count > 0:
if (listA[count-1] in listB) == True:
same.append(listA[count-1])
count = count -1
return same
def compareDiff(listA, listB):
diff = []
count = len(listA)
while count > 0:
if (listA[count-1] in listB) == False:
diff.append(listA[count-1])
count = count -1
return diff
#Prep for report#
same = compareSame(dirA, dirB)
diffA = compareDiff(dirA, dirB)
diffB = compareDiff(dirB, dirA)
#Print report#
print "#########--SAME in both directories--#########"
for i in same:
print "%s: is a common file" % (i,)
print '#########--Unique in "'+sys.argv[1]+'"--#########'
for i in diffA:
print "%s: is a unique file in directory %s" % (i, ('"'+sys.argv[1]+'"'))
print '#########--Unique in "'+sys.argv[2]+'"--#########'
for i in diffB:
print "%s: is a unique file in directory %s" % (i, ('"'+sys.argv[2]+'"'))