Python - 在多个文件比较后,将结果写入多个文件

时间:2017-05-11 03:39:08

标签: python

我有3个文件夹,我需要逐个比较folder1和folder2中的文件,然后将差异的结果逐个写入folder3中的文件。现在我有这样的脚本:

import os
import difflib
import sys
path1 = "D:\folder1"
path2 = "D:\folder2"
path3 = "D:\folder3"
dirs1 = os.listdir(path1)
dirs2 = os.listdir(path2)
dirs3 = os.listdir(path3)
for file_name in dirs1:
  file_1_path = os.path.join(path1, file_name)
  name,ext = os.path.splitext(file_name)
  file_2_path = os.path.join(path2, name+str(1)+ext)
  file_3_path = os.path.join(path3, name+str('-Result')+ext)
  with open (file_1_path) as xmlfile1:
    with open (file_2_path) as xmlfile2:
      line_num1=sum(1 for line1 in xmlfile1)
      line_num2=sum(1 for line2 in xmlfile2)
      xmlfile1.seek(0)
      xmlfile2.seek(0)
      diffInstance = difflib.Differ()
      diffList = list(diffInstance.compare(xmlfile1.readlines(), xmlfile2.readlines()))
      with open (file_3_path) as result:
        for i,line in enumerate(diffList):
          result.write(line)
          if line_num1 == line_num2:
            if line.startswith('+'):
              sys.stdout.write('Current version Line: {} , text: {}'.format(i-1,line))
            elif line.startswith('-'):
              sys.stdout.write('Previous version Line: {}, text: {}'.format(i+1,line))
          if line_num1 > line_num2:
            x = line_num1-line_num2
            if line.startswith('-'):
              sys.stdout.write('Missed Line: {} from previous version, text: {}'.format(i+1,line))
          if line_num1<line_num2:
            y = line_num2-line_num1
            if line.startswith('+'):
              sys.stdout.write('Extra Line: {} in currect version, text: {}'.format(i+1,line))

但似乎效果不好,我得到了这样的错误:

Traceback (most recent call last):
File "C:/comparison.py", line 25, in <module>
        result.write(line)
    io.UnsupportedOperation: not writable

1 个答案:

答案 0 :(得分:1)

仅使用一个参数的open采用r模式并使用with open (file_3_path) as result:。当然,在读取模式下对文件对象使用write将无法正常工作。将其更改为with open (file_3_path, 'w') as result:,文件将是可写的。在上下文中:

import os
import difflib
import sys
path1 = "D:\folder1"
path2 = "D:\folder2"
path3 = "D:\folder3"
dirs1 = os.listdir(path1)
dirs2 = os.listdir(path2)
dirs3 = os.listdir(path3)
for file_name in dirs1:
  file_1_path = os.path.join(path1, file_name)
  name,ext = os.path.splitext(file_name)
  file_2_path = os.path.join(path2, name+str(1)+ext)
  file_3_path = os.path.join(path3, name+str('-Result')+ext)
  with open (file_1_path) as xmlfile1:
    with open (file_2_path) as xmlfile2:
      line_num1=sum(1 for line1 in xmlfile1)
      line_num2=sum(1 for line2 in xmlfile2)
      xmlfile1.seek(0)
      xmlfile2.seek(0)
      diffInstance = difflib.Differ()
      diffList = list(diffInstance.compare(xmlfile1.readlines(), xmlfile2.readlines()))
      with open (file_3_path, 'w') as result:
        for i,line in enumerate(diffList):
          result.write(line)
          if line_num1 == line_num2:
            if line.startswith('+'):
              sys.stdout.write('Current version Line: {} , text: {}'.format(i-1,line))
            elif line.startswith('-'):
              sys.stdout.write('Previous version Line: {}, text: {}'.format(i+1,line))
          if line_num1 > line_num2:
            x = line_num1-line_num2
            if line.startswith('-'):
              sys.stdout.write('Missed Line: {} from previous version, text: {}'.format(i+1,line))
          if line_num1<line_num2:
            y = line_num2-line_num1
            if line.startswith('+'):
              sys.stdout.write('Extra Line: {} in currect version, text: {}'.format(i+1,line))