Внесение изменений/замена текста в файле в определенных местах в python

<
import os

project_name = str(input("Whats your project name : ")).capitalize()
apps_name = str(input("What your apps name : ")).split(",")


# check if the project name is in the current directory
if os.path.isdir(project_name):
    print("Project name is already in the current directory")
    exit()
else:
    os.system('cmd /c "django-admin startproject ' + project_name)
    # change the directory to the project name
    os.chdir(project_name)
    # print the current directory
    print(os.getcwd())
    # check if the apps name is in the project directory
    if os.path.isdir(apps_name[0].capitalize()):
        print("Apps name is already in the project directory")
        exit()
    else:
        # check if apps_name has more than one app
        if len(apps_name) > 1:
            for app in apps_name:
                os.system('cmd /c "django-admin startapp ' + app.capitalize())
        else:
            os.system('cmd /c "django-admin startapp ' + apps_name[0].capitalize())
# change the directory to the project name
os.chdir('../'+project_name)
# print the current directory
print(os.getcwd())

search_text = '\nINSTALLED_APPS = [\n"django.contrib.admin",\n"django.contrib.auth",\n"django.contrib.contenttypes",\n"django.contrib.sessions",\n"django.contrib.messages",\n"django.contrib.staticfiles"]\n'
for app in apps_name:
    search_text += '"' + app.capitalize() + '.apps.' +  app.capitalize() +'Config",\n'
replace_text = search_text + ']\n'

print(replace_text)
# append the apps name to INSTALLED_APPS in settings.py
with open(project_name + '/settings.py', 'r+') as file:
    content=file.read()
    content.split(search_text)
    new_file = content[0] + replace_text + content[1]
    # content = content.replace(search_text, replace_text)
    # for app in apps_name:
    #     file.write('"' + app.capitalize() + '.apps.' +  app.capitalize() +'Config",\n')
    file.write(new_file)
    file.truncate()

Мне нужна ваша помощь. В настоящее время я разрабатываю сценарий автоматизации Django, который пригодится в будущем. Однако я не могу заменить определенный фрагмент кода на свой обновленный фрагмент кода. Обновленные фрагменты кода записываются в конце файла, удваивая фрагмент кода в том же файле. Вы можете увидеть код, который мы (я и Github Copilot) написали вместе

Вернуться на верх