I have written this short code
import os
import os.path
import string
import shutil
import difflib
Path = 'C:/RESULT/BATCH/SimpleInputParser/'
SecPoint = []
FullList = []
newFile = []
for fileName in os.listdir(Path):
if fileName.endswith(".inp"):
f=open(Path + fileName, 'r')
files = f.readlines()
f.close()
fp_clean = open(Path + "tempClean.inp", "w")
fp_newFile = open(Path + "newFile.inp", "w")
for i, line in enumerate(files):
if line.startswith(("*SECTION POINTS")):
SecPoint.append(files[i].strip())
SecPoint.append(files[i+1].strip())
newFile.append(line.strip())
fp_clean.write('\n'.join(SecPoint).strip()+'\n')
fp_clean.close()
fp_newFile.write('\n'.join(newFile).strip())
fp_newFile.close()
f1 = open(Path + "newFile.inp", "r")
fileOne = f1.readlines()
f1.close()
f2 = open(Path + "tempClean.inp", "r")
fileTwo = f2.readlines()
f2.close()
outFile = open(Path + fileName.split(".")[0] + "_v01.inp", "w")
diffInstance = difflib.Differ()
diffList = list(diffInstance.compare(fileOne, fileTwo))
for line in diffList:
if line[0] =='-':
FullList.append(line.split("-")[1].strip())
outFile.write('\n'.join(FullList).strip())
outFile.close()
SecPoint = []
FullList = []
newFile = []
if os.path.exists(Path + "tempClean.inp"):
os.remove(Path + "tempClean.inp")
else:
break
if os.path.exists(Path + "newFile.inp"):
os.remove(Path + "newFile.inp")
else:
break
When I compare the outFile with the input, I have notice that python is substituting all the lines that starts with "-" (minus, negative numbers) with blank lines.
Can you tell me why and how to fix it? I have to use 2.6.2 python
Thanks