I am trying to remove SQL comments /**/ and -- from text file. Below code works and removes comments, if I store hard-coded SQL comment data in filedata variable. but doesn't work on file if I store it with read(). Also, tried regular expression bit doesn't work.
import re
import os
from os import path
import shutil
from pathlib import Path
import pyparsing
dst = "C:\\Users\\user1\\Desktop\\temp\\XYZ\\"
src = "C:\\Users\\user1\\Desktop\\temp\\ABC\\"
with open("C:\\Users\\user1\\Desktop\\SPs\\sample.txt", "r") as a_file:
for line in a_file:
#reading filename from text file
stripped_line = line.strip()
try:
Sfile = open(src + stripped_line, "r")
Ofile = open(dst + stripped_line, 'w', newline='')
filedata = str(Sfile.read())
#here, if I put hard-coded SQL commented string in filedata, it works and removes comments but it doesn't work if I read data from file as above and put in filedata variable.
filedata1 = pyparsing.nestedExpr("/*", "*/").suppress()
Ofile.write(filedata1.transformString(filedata))
Ofile.close()
Sfile.close()
except Exception:
print('Error:'+stripped_line+' not found')
print('Successfully completed!')