Question

I have the below code for comparing a number of files two by two, and I want to write my results (the last two lines) to a text file. But the outcome file is empty. Anyone has any idea why?

import difflib
from os import listdir
from os.path import isfile, join


mypath="D:\correctfiles\\112\\"
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]



length = len(onlyfiles)
for i in range(0, length):
    for j in range(i + 1, length):
        fi = open(mypath + onlyfiles[i])
        fj = open(mypath + onlyfiles[j])
        ilines= fi.readlines()
        jlines= fj.readlines()
        d = difflib.Differ()
        diff_list = list(d.compare(ilines, jlines))
        n_adds, n_subs, n_eqs, n_wiered = 0, 0, 0, 0
        for diff_item in diff_list:
            if diff_item[0] == '+':
                n_adds += 1
            elif diff_item[0] == '-':
                n_subs +=1 
            elif diff_item[0] == ' ':
                n_eqs += 1
            else: 
                n_wiered += 1



        if n_eqs >= (len(ilines))/2 or n_eqs >= (len(jlines))/2:
            if n_eqs != 0:
                fh= open('D:\\outcome.txt', 'w')


                print 'lines files %s: %d  %s: %d' % (onlyfiles[i],len(ilines),onlyfiles[j], len(jlines))
                print 'adds: %d subs: %d eqs: %d ?:%d '  % (n_adds, n_subs, n_eqs, n_wiered)
                fh.close()

No correct solution

OTHER TIPS

You are printing your text, not writing it to the file object. Use fh.write() to write lines to the file:

with open('D:\\outcome.txt', 'a') as fh:
    fh.write('lines files %s: %d  %s: %d\n' % (onlyfiles[i],len(ilines),onlyfiles[j], len(jlines)))
    fh.write('adds: %d subs: %d eqs: %d ?:%d\n' % (n_adds, n_subs, n_eqs, n_wiered))

Alternatively, use the (more obscure and little-used) >> print redirection syntax:

with open('D:\\outcome.txt', 'a') as fh:
    print >> fh, 'lines files %s: %d  %s: %d' % (onlyfiles[i],len(ilines),onlyfiles[j], len(jlines)))
    print >> fh, 'adds: %d subs: %d eqs: %d ?:%d' % (n_adds, n_subs, n_eqs, n_wiered))

In both cases, I open the file here in append mode, to add new information rather than replace the file contents.

Alternatively, move the file opening outside of the for loops to truncate it just once, then write lines to the file each loop iteration before closing it once after the loop has completed. The with statement takes care of closing the file object for you when the with block completes or is exited.

This part here:

print 'lines files %s: %d  %s: %d' % (onlyfiles[i],len(ilines),onlyfiles[j], len(jlines))
print 'adds: %d subs: %d eqs: %d ?:%d '  % (n_adds, n_subs, n_eqs, n_wiered)

Prints the output to console, not to the file.

You can print it to the file, change it to this:

Python 2.x

print >> fh, 'lines files %s: %d  %s: %d' % (onlyfiles[i],len(ilines),onlyfiles[j], len(jlines))
print >> fh, 'adds: %d subs: %d eqs: %d ?:%d '  % (n_adds, n_subs, n_eqs, n_wiered)

Bonus: Python 3.x - In case someone needs it :)

print('lines files %s: %d  %s: %d' % (onlyfiles[i],len(ilines),onlyfiles[j], len(jlines)), file=fh)
print('adds: %d subs: %d eqs: %d ?:%d '  % (n_adds, n_subs, n_eqs, n_wiered), file=fh)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top