import secrets import os def generate_secret_key(): return secrets.token_hex(16) # 16 bytes = 32 hex characters def update_env_file(secret_key): env_path = os.path.join(os.path.dirname(__file__), '.env') with open(env_path, 'r') as file: lines = file.readlines() with open(env_path, 'w') as file: for line in lines: if line.startswith('SECRET_KEY='): file.write(f'SECRET_KEY={secret_key}\n') else: file.write(line) if __name__ == "__main__": new_secret_key = generate_secret_key() print(f"Generated Secret Key: {new_secret_key}") update_env_file(new_secret_key) print("The .env file has been updated with the new SECRET_KEY.")