import re with open('web_app.py', 'r', encoding='utf-8') as f: content = f.read() # Match any broken variant of the multipart yield — # captures everything between 'yield (b\'--frame' and the closing ')' # regardless of how the newlines/whitespace got mangled. pattern = re.compile( r"[ \t]*yield\s*\(b'--frame.*?buffer\.tobytes\(\).*?\)", re.DOTALL ) fixed_line = r" yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + buffer.tobytes() + b'\r\n')" match = pattern.search(content) if match: content = pattern.sub(fixed_line, content, count=1) with open('web_app.py', 'w', encoding='utf-8') as f: f.write(content) print(f"Fixed! Replaced:\n {match.group()!r}\nWith:\n {fixed_line!r}") else: print("ERROR: Could not find the broken yield statement. Check the file manually.") # Print nearby context to help debug for i, line in enumerate(content.splitlines(), 1): if 'frame' in line or 'tobytes' in line: print(f" Line {i}: {line!r}")