file comparisons

S

Scott Carlson

Still new to Python, but liking it.

Using Windows.

Synopsis:

I create some files in a Temporary Folder while doing some work.

Then, I want to look in a (different)Primary Folder to see if
similarly named Files exist.

If I find two Files with the same name, I want to know if the contents
of the Files are exactly the same (I don't care about the date/time of
the File).

If they are different I want to move the new one into the Primary
Folder.


I'm thinking there's a nice concise way to do this.

Thanks,
Scott Carlson
 
I

Irmen de Jong

Scott said:
If I find two Files with the same name, I want to know if the contents
of the Files are exactly the same (I don't care about the date/time of
the File).

If they are different I want to move the new one into the Primary
Folder.

Something like:-

find files to process using os.listdir() on both directories
and checking for common files, for instance:
commonfiles=sets.Set(os.listdir("tempdir")) & sets.Set(os.listdir("primarydir"))

Loop over the files using os.path.isfile (so you skip the
directories, then for each file read both files into memory using....
f1=open( ....tempfilename.... , "rb").read()
f2=open( ....primaryfilename... ,"rb").read()

Then compare and move file if unequal:
if f1!=f2:
shutil.move("filename1","filename2")

(Untested)

--Irmen
 
I

Irmen de Jong

Irmen de Jong wrote:
[a lot of stuff]

Argh. Just saw Rick's reply; I didn't know about the
filecmp module.... :-| So this makes it even simpler :)

--Irmen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,684
Latest member
sparada

Latest Threads

Top