Spaces:
Running
Running
File size: 1,041 Bytes
20ee727 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import os
env_path = ".env"
key_line = "OPENAI_API_KEY=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"
try:
# Read existing content
if os.path.exists(env_path):
with open(env_path, "r", encoding="utf-8", errors="ignore") as f:
lines = f.readlines()
else:
lines = []
# Check if key exists
found = False
new_lines = []
for line in lines:
if line.strip().startswith("OPENAI_API_KEY="):
new_lines.append(key_line + "\n")
found = True
else:
new_lines.append(line)
if not found:
if new_lines and not new_lines[-1].endswith("\n"):
new_lines[-1] += "\n"
new_lines.append(key_line + "\n")
# Write back
with open(env_path, "w", encoding="utf-8") as f:
f.writelines(new_lines)
print("Fixed .env with UTF-8")
except Exception as e:
print(f"Error fixing .env: {e}")
|