Moreover, if you do not write the received data to a file, but simply display it on the screen, then the script works great.
Ok, try this one,
With open("S-1.txt","a") as file:
file.write(str(i)+" = "+str(C)+"\n")
file.close()
With open("D-1.txt","a") as file:
file.write(str(i)+" = "+str(D)+"\n")
file.close()
If it didn't work, appeal to our future overlord aka AD(artificial dumbness).😉