- 论坛徽章:
- 0
|
本帖最后由 t6760915 于 2010-06-01 17:24 编辑
- #coding=utf-8
- import os
- import sys
- class cmp:
- adict = {}
- bdict = {}
- def __init__(self):
- self.adict = self.loadFile('a.txt')
- self.bdict = self.loadFile('b.txt')
- def loadFile(self, path):
- if not path:
- return False
-
- if not os.path.exists(path):
- return False
-
- try:
- fp = open(path, 'r')
-
- lines = []
- for line in fp:
- lines.append(line.strip())
- fp.close()
- except:
- return False
-
- if not lines:
- return False
-
- names = []
- values = []
- for linnnum,line in enumerate(lines):
- if not line:
- continue
-
- if linnnum % 2 == 0:
- names.append(line)
- else:
- values.append(line)
-
- return dict(zip(names, values))
- def run(self):
- for key,val in self.adict.items():
- if not self.bdict.has_key(key):
- print 'key %s not exists in b.txt' % key
- else:
- if self.bdict[key] == val:
- print 'key %s eqal in a.txt and b.txt' % key
- else:
- print 'key %s not eqal in a.txt and b.txt' % key
- def __del__(self):
- self.adict = {}
- self.bdict = {}
- if __name__ == '__main__':
- c = cmp()
- c.run()
复制代码
cmp.rar
(999 Bytes, 下载次数: 46)
- D:\备份\cmp>python cmp.py
- key @080404_HWI-EAS121_0001FC209NNAAXX_4_1_905_794 eqal in a.txt and b.txt
- key @080404_HWI-EAS121_0001FC209NNAAXX_4_3_905_794 not exists in b.txt
- key @080404_HWI-EAS121_0001FC209NNAAXX_4_1_931_319 not eqal in a.txt and b.tx
- key @080404_HWI-EAS121_0001FC209NNAAXX_4_1_895_807 not eqal in a.txt and b.tx
- key @080404_HWI-EAS121_0001FC209NNAAXX_4_1_906_759 eqal in a.txt and b.txt
- D:\备份\cmp>
复制代码 |
|