class C:
pass
file = open("newfile.txt", "w")
for j in range(10):
c = C()
print c
file.write(c)
file.close()
Is there anything wrong in this code?
I am new to python and want to write the content that's outputted by 'print c' to file ?
print >>file, cto send the output of aprintstatement to the file. In Python 3 theprint()function uses a keyword argument to do this, i.e.print(c, file=file). Afile'swrite()method does not automatically call an object's__str__()method likeprintdoes, so you'd need do it explicitly viafile.write(str(c)+'\n').