User Name
Password
AppleNova Forums » Programmer's Nook »

Python -- Directory content differences


Register Members List Calendar Search FAQ Posting Guidelines
Python -- Directory content differences
Thread Tools
Wickers
is not a kind of basket
 
Join Date: May 2004
 
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.

Code:
"""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]+'"'))

no sig, how's that for being a rebel!
  quote
wtd
Member
 
Join Date: Jul 2004
 
2005-11-16, 20:28

You may wish to look into using the built-in set type to make this easier.

For instance, finding unique elements in one of two lists can be quite easy.

Code:
>>> a, b = set([1,2,3]), set([3,4, 1]) >>> a ^ b & a set([2]) >>> b ^ a & b set([4])
  quote
Wickers
is not a kind of basket
 
Join Date: May 2004
 
2005-12-19, 03:58

Ahh...

Since I had no clue what sets were, till a few minutes ago. (I did a >>>help(set))

Well this will cut a few lines out, but I'm going to use the set.differences() dealie.

Thanks.

no sig, how's that for being a rebel!
  quote
Barto
Student extraordinaire
 
Join Date: May 2004
Location: Canberra, Australia
 
2005-12-19, 04:38

Tiger's version of python doesn't work with sets, at least not for me. Works fine for me in SuSE 10.0.

Code:
NameError: name 'set' is not defined
  quote
Wickers
is not a kind of basket
 
Join Date: May 2004
 
2005-12-19, 11:26

That's interesting...
Remind me again of which version tiger has installed?
2.3?

Sets work fine for me under 2.4.1, (with SuSE 10, and Slackware)
  quote
Wickers
is not a kind of basket
 
Join Date: May 2004
 
2005-12-19, 11:44

Oh, this is odd... how would I turn a set, back into just a list?
I mean, aside from how I'm currently doing it that is:
Code:
example = [] for i in set(var): example.append(i)
Is there something built into sets that can call just a list?

no sig, how's that for being a rebel!
  quote
wtd
Member
 
Join Date: Jul 2004
 
2005-12-21, 18:11

Code:
>>> list(set([1,2,3])) [1, 2, 3]
  quote
wtd
Member
 
Join Date: Jul 2004
 
2005-12-21, 18:13

But why do you need it as a list? You're likely overlooking some of Python's power.
  quote
Wickers
is not a kind of basket
 
Join Date: May 2004
 
2005-12-24, 02:27

Sweet, so I replaced the compare code with this.

Code:
def cSame(listA, listB): listA, listB = set(listA), set(listB) return list(listA.intersection(listB)) def cDiff(listA, listB): listA, listB = set(listA), set(listB) return list(listA.difference(listB))
The reports now look a bit more 'formatted' too, I'll post the new script.
  quote
Wickers
is not a kind of basket
 
Join Date: May 2004
 
2005-12-24, 02:31

I wanted to just edit my parent post and replace the script there... but I'm not able to do so.
So I'll just chalk up another post in this thread.

Code:
"""dirdiff.py: List common and uncommon files and subdirectories between two given directories. Usage: #python dirdiff.py [dir...] [dir...] v0.0.2a --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 cSame(listA, listB): listA, listB = set(listA), set(listB) return list(listA.intersection(listB)) def cDiff(listA, listB): listA, listB = set(listA), set(listB) return list(listA.difference(listB)) #Misc. report prep# def spaces(number): string = "" while number > 0: string = string + " " number = number -1 return string #Print report# print "#########--SAME in both directories--#########" for i in cSame(dirA, dirB): print "# %s %s --is a common file" % (i, spaces(20 - len(i))) print '#########--Unique in "'+sys.argv[1]+'"--#########' for i in cDiff(dirA, dirB): print "# %s %s --is a unique file in directory %s"\ % (i, spaces(20 - len(i)), ('"'+sys.argv[1]+'"')) print '#########--Unique in "'+sys.argv[2]+'"--#########' for i in cDiff(dirB, dirA): print "# %s %s --is a unique file in directory %s" \ % (i, spaces(20 - len(i)), ('"'+sys.argv[2]+'"'))

You'll see the difference between the two scripts's output if you run it...

no sig, how's that for being a rebel!
  quote
wtd
Member
 
Join Date: Jul 2004
 
2005-12-27, 01:56

Code:
def spaces(number): string = "" while number > 0: string = string + " " number = number -1 return string
Or perhaps you prefer:

Code:
' ' * number
  quote
wtd
Member
 
Join Date: Jul 2004
 
2005-12-27, 02:01

You may also wish to rewrite some of those functions to take advantage of generators.

Code:
def cSame(listA, listB): listA, listB = set(listA), set(listB) return list(listA.intersection(listB)) def cDiff(listA, listB): listA, listB = set(listA), set(listB) return list(listA.difference(listB))
Code:
def cSame(listA, listB): listA, listB = set(listA), set(listB) for x in listA & listB: yield x def cDiff(listA, listB): listA, listB = set(listA), set(listB) for x in listA ^ listB & listA: yield x
If you have directories with a huge number of files, using a generator can mean better memory efficiency that simply returning the entire list.
  quote
Wickers
is not a kind of basket
 
Join Date: May 2004
 
2006-03-19, 13:32

Quote:
Originally Posted by wtd
Code:
' ' * number
Yeah, I just learned about that type of duplication. Thanks
(gee as time goes on this script will be a one liner hehe...)
  quote
Posting Rules Navigation
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
Thread Tools
Similar Threads
Thread Thread Starter Forum Replies Last Post
Content Server for Plasma Screen Bryson Purchasing Advice 0 2005-10-31 09:55
Python Failure after Tiger Upgrade Caseyfern Genius Bar 0 2005-05-23 11:36
How Can You Reconstruct the File Directory After Disk Utility? SilverGoat Genius Bar 0 2005-03-06 08:27


« Previous Thread | Next Thread »

All times are GMT -5. The time now is 02:01.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2024, AppleNova