473,378 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

check if files are the same on Windows

A crude way to check if two files are the same on Windows is to look
at the output of the "fc" function of cmd.exe, for example

def files_same(f1,f2):
cmnd = "fc " + f1 + " " + f2
return ("no differences" in popen(cmnd).read())

This is needlessly slow, because one can stop comparing two files
after the first difference is detected. How should one check that
files are the same in Python? The files are plain text.

Mar 19 '07 #1
3 2907
In <11*********************@y66g2000hsf.googlegroups. com>, Beliavsky
wrote:
[…] How should one check that files are the same in Python? The files
are plain text.
Take a look at the `filecmp` module. Pay attention to the shallow
argument of `filecmp.cmp()` and the default value!

Ciao,
Marc 'BlackJack' Rintsch

Mar 19 '07 #2
In the unix world, 'fc' would be like diff.

"""
Python example of checksumming files with the MD5 module.

In Python 2.5, the hashlib module would be preferable/more elegant.
"""

import md5

import string, os
r = lambda f: open(f, "r").read()
def readfile(f,strip=False): return (strip and stripper(r(f))) or r(f)
def writefile(f, data, perms=750): open(f, "w").write(data) and
os.chmod(f, perms)

def get_md5(fname):
hash = md5.new()
contents = readfile(fname)
hash.update(contents)
value = hash.digest()
return (fname, hash.hexdigest())

import glob

for f in glob.glob('*'):
print get_md5(f)
A crude way to check if two files are the same on Windows is to look
at the output of the "fc" function of cmd.exe, for example

def files_same(f1,f2):
cmnd = "fc " + f1 + " " + f2
return ("no differences" in popen(cmnd).read())

This is needlessly slow, because one can stop comparing two files
after the first difference is detected. How should one check that
files are the same in Python? The files are plain text.

--
Shane Geiger
IT Director
National Council on Economic Education
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
Mar 19 '07 #3
On Mar 19, 11:55 am, Shane Geiger <sgei...@ncee.netwrote:
In the unix world, 'fc' would be like diff.

"""
Python example of checksumming files with the MD5 module.

In Python 2.5, the hashlib module would be preferable/more elegant.
"""

import md5

import string, os
r = lambda f: open(f, "r").read()
def readfile(f,strip=False): return (strip and stripper(r(f))) or r(f)
def writefile(f, data, perms=750): open(f, "w").write(data) and
os.chmod(f, perms)

def get_md5(fname):
hash = md5.new()
contents = readfile(fname)
hash.update(contents)
value = hash.digest()
return (fname, hash.hexdigest())

import glob

for f in glob.glob('*'):
print get_md5(f)
A crude way to check if two files are the same on Windows is to look
at the output of the "fc" function of cmd.exe, for example
def files_same(f1,f2):
cmnd = "fc " + f1 + " " + f2
return ("no differences" in popen(cmnd).read())
This is needlessly slow, because one can stop comparing two files
after the first difference is detected. How should one check that
files are the same in Python? The files are plain text.

--
Shane Geiger
IT Director
National Council on Economic Education
sgei...@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

sgeiger.vcf
1KDownload
You can also use Python's file "read" method to read a block of each
file in a loop in binary mode.

Something like:

file1 = open(path1, 'rb')
file2 = open(path2, 'rb')

bytes1 = file1.read(blocksize)
bytes2 = file2.read(blocksize)

And then just compare bytes to see if there is a difference. If so,
break out of the loop. I saw this concept in the book: Python
Programming, 3rd Ed. by Lutz.

Have fun!

Mike

Mar 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: martijn | last post by:
Hi There, my vb app can open files. Well, actually my vb app launches word or excell to open them. These files are shared in a network. If somebody else has allready opened that file, you...
4
by: Rob | last post by:
I have an Access application that allows users to open up certain files from within Access (.doc files with automation, .pdf's with a pdf viewing form). Other formats, we want to use Internet...
19
by: wetherbean | last post by:
Hi group..I am writing a playlist management protocol where I have a file that holds all the playlists and a file that holds all the songs....before a playlist is created I need to check to see if...
2
by: Guy_B | last post by:
Hi I am using the filesystem watcher to pick up files as they are dropped into a folder - the files are output file from another system and I have no control over that. When 'watcher raises the...
12
by: tshad | last post by:
What would be a good way to check programmatically whether a service was running? We have a service that dies periodically and I need to check to see if this service is running. I know how to...
9
by: Mark | last post by:
Hi all, This is something which has been bugging me for ages. How can I check if a file is already in use by a different program? It doesn't seem to matter which mode I pass to fopen, it will...
6
by: Fred W. | last post by:
When my application starts I need to check folder permissions to ensure they have "Full Control" before I let them proceed on. How can I check this permission. Thank you, Fred
6
by: Ros | last post by:
There are 10 files in the folder. I wish to process all the files one by one. But if the files are open or some processing is going on them then I do not want to disturb that process. In that case...
0
by: Ralf Abramowitsch | last post by:
Hi, I'm writing a trouble shooting utility that checks my assembly references. It creates a report for the support. Now I want to add some routines to check for installed runtimes. Ok, the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.