Python中对文件的比较操作

import os
ftype=raw_input('The extension for known file types:')
filename1=raw_input('Filename1:')
f1=open(filename1+ftype,'r')
alllines1=f1.readlines()
f1.close()

filename2=raw_input('Filename2:')
f2=open(filename2+ftype,'r')
alllines2=f2.readlines()
f2.close()
i=0
duodei=0
ladd=[]
for s1 in alllines1:
    for s2 in alllines2:
        if cmp(s1,s2)==0:
            i=0
            break
        else:
            i+=1
    if i!=0:
        duodei+=1
        ladd.append(s1)
s1='which %s have but %s not ' % (filename1,filename2)
s1+=os.linesep
ladd.append(s1)
s='%s to %s is %d' % (filename1,filename2,duodei)
s+=os.linesep
ladd.append(s)

j=0
duodej=0
for ss2 in alllines2:
    for ss1 in alllines1:
        if cmp(ss2,ss1)==0:
            j=0
            break
        else:
            j+=1
    if j!=0:
        duodej+=1
        ladd.append(ss2)
ss2='which %s have but %s not ' % (filename2,filename1)
ss2+=os.linesep
ladd.append(ss2)
ss='%s to %s is %d' % (filename2,filename1,duodej)
ss+=os.linesep
ladd.append(ss)

fname=raw_input('Filename out:')
fadd=open(fname+ftype,'w')
fadd.writelines(['%s' % x for x in ladd])
for x in ladd:
    print '%s' % x
#print ['%s' % x for x in ladd]
fadd.close()
filename=raw_input('OK')

你可能感兴趣的:(Python中对文件的比较操作)