Gabe
Gabe

Reputation: 549

How to test a folder for new files using python

How would you go about testing to see if 2 folders contain the same files, and then to be able to manipulate ONLY the file which is new.

A = listdir('C:/')
B = listdir('D:/')

If A==B

...

I know this could be used to test if directories are different but is there a better way? And if A and B are the same, except B has one more file than A, how do i use just the new file?

Thank you, i hope my question isnt confusing

Upvotes: 2

Views: 1857

Answers (2)

S.Lott
S.Lott

Reputation: 391854

http://docs.python.org/library/filecmp.html

http://docs.python.org/library/filecmp.html#the-dircmp-class

import filecmp
compare = filecmp.dircmp( "C:/", "D:/" )
for f in compare.left_only:
    print "C: new", f
for f in compare.right_only:
    print "D: new", f

Upvotes: 8

nosklo
nosklo

Reputation: 222852

A = set(os.listdir('C:\\'))
B = set(os.listdir('D:\\'))

print 'Files in A but not in B:', A - B
print 'Files in B but not in A:', B - A

Upvotes: 4

Related Questions