import re with open('addons/education_lms/security/security.xml', 'r') as f: text = f.read() # Replace Course rule domain with just state text = re.sub( r'.*?\[.*?\]', """ Student Course Access [ ('state', '=', 'published'), ]""", text, flags=re.DOTALL ) with open('addons/education_lms/security/security.xml', 'w') as f: f.write(text)