Python Puzzler

This one took me way longer that it should have to figure out. Granted, it was buried in a test case in larger body of code, and a fresh look after a nights sleep solved it, but still…… YMMV

The problem? A simple file comparison was failing intermittently. First, an example of the code in question:

$ cat file_compare.py 
import filecmp
import sys

# create a reference data file
data = "Look, that rabbit's got a vicious streak a mile wide! It's a killer!\n" * 1975
reference = 'quote.txt'
rf = open(reference, 'w')
rf.write(data)
rf.close()

# write a bunch of files, and make sure they 
# match the reference data
for i in range(10):
    filename = 'quote%d.txt' % i
    df = open(filename, 'w') 
    df.write(data)
    df.close
    if filecmp.cmp(reference, filename) is not True:
        print 'Error: Files are different in pass %d!' % i
        sys.exit()
print 'All files are the same like they should be.'

Running the test case :

$ python file_compare.py 
Error: Files are different in pass 0!
$ python
Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04) 
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import filecmp
>>> filecmp.cmp('quote.txt', 'quote0.txt')
True
>>> 

Hint

Update: The actual code was in test_XmlWrite.py, line 563.


Subscribe to Blog Updates

Quickly connect apps, data, and devices

Start Free Trial
Contact Us Free Trial