Write expr.txt and curve.png.
Goal
Print the file contents and save a figure.
from pathlib import Path
import sympy as sp
x = sp.symbols('x')
Path('expr.txt').write_text(str(sp.factor(x ** 2 - 5 * x + 6)), encoding='utf-8')
print(Path('expr.txt').read_text(encoding='utf-8'))import numpy as np
import sympy as sp
x = sp.symbols('x')
f = sp.lambdify(x, x ** 2 - 5 * x + 6, 'numpy')
xs = np.linspace(-1, 6, 80)
plt.plot(xs, f(xs))
plt.savefig('curve.png', dpi=120, bbox_inches='tight')
plt.show()
print('wrote curve.png')