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 
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')

# 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') 
    if filecmp.cmp(reference, filename) is not True:
        print 'Error: Files are different in pass %d!' % i
print 'All files are the same like they should be.'

Running the test case :

$ python 
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')


Update: The actual code was in, line 563.

Category: Product
Topics: API

We're hiring!

Search and find your once in a lifetime opportunity.

View current openings
Contact Us Free Trial