0

By using below code, I copy the file content from CONFIG.txt to CONFIG_shorted.txt by using shutil.copy. After that, trying to find the search keywords (pattern) in CONFIG_shorted.txt and replace with Short names (Shorted_Dir_Name_List). but line is not getting replaced, instead new lines are added at end of the file CONFIG_shorted.txt with \t \n characters, how to avoid special symbols?

for root, dirs, files in os.walk('.'):
    for i in files:
        if i == 'CONFIG.txt':
            old_name = os.path.join(root, i)
            #base, extension = os.path.splitext(i)
            new_name = os.path.join(root, 'CONFIG_shorted.txt')
            if not os.path.exists(new_name):
                shutil.copy(old_name, new_name)
                f = open(new_name, 'r+')
                with f as myFile:
                    for line in enumerate(myFile):
                        for j in range(len(Dir_Name_List)):
                            pattern = "/" + Dir_Name_List[j] + "/"
                            if pattern in str(line):
                                line = str(line).replace(pattern, "/" + Shorted_Dir_Name_List[j] + "/")
                        f.write(str(line))
            f.close()

For your reference: Dir_Name_List[] = 'Source_Library', 'STD_Lib' Shorted_Dir_Name_List[] = 'S_L', 'STD_L'

CONFIG.txt:

{

                "precompile_headers": [
                    "${ROOT}/Source_Library/STD_Lib/a.h",
                    "${ROOT}/Source_Library/STD_Lib/a.c"
                ]
}

Output i'm getting is: CONFIG_shorted.txt

{

                "precompile_headers": [
                    "${ROOT}/Source_Library/STD_Lib/a.h",
                    "${ROOT}/Source_Library/STD_Lib/a.c"
                ]
}(0, '{\n')(1, '\n')(2, '\t\t\t\t"precompile_headers": [\n')(3, '\t\t\t\t\t"${ROOT}/S_L/STD_L/a.h",\n')(4, '\t\t\t\t\t"${ROOT}/S_L/STD_L/a.c",\n')(5, '\t\t\t\t]\n')(6, '}\n')

Expected output is: CONFIG_shorted.txt

{

                "precompile_headers": [
                    "${ROOT}/S_L/STD_L/a.h",
                    "${ROOT}/S_L/STD_L/a.c"
                ]
}
  • 1
    You copy the full file to a new name then open it with `f = open(new_name, 'r+')` ... do you know what `r+` does? If not, why not look it up? If you want to rewrite the full file anyway, why not create an empty new file and copy line by line? – Patrick Artner May 10 '22 at 08:53
  • 1
    `f.write(str(line))` => line is a string already, why get the str() of it?# – Patrick Artner May 10 '22 at 08:54
  • I'm getting error: TypeError: write() argument must be str, not tuple. So i used str(). Is this correct? I have used to open a file in r+ to read and write. As you suggested I have ignored 'shutil.copy' and new_name = os.path.join(root, 'CONFIG_shorted.txt') and newFile = open(new_name, 'w') – jayasimha kumar May 10 '22 at 10:28
  • You create the tuples yourself `for line in enumerate(myFile):` will give you each line of the file as tuple `(lineNr, lineContent)` you never use the linenumber for enything - why enumerate? if you really need it, use `for line_number, line_content in enumerate(myFile):` and only write the line_content to the new file .... – Patrick Artner May 10 '22 at 15:28

0 Answers0