I am generating some numbers (let’s say, num) and writing the numbers to an output file using outf.write(num).
But the interpreter is throwing an error:
outf.write(num)
TypeError: argument 1 must be string or read-only character buffer, not int.
How can I solve this problem?
write() only takes a single string argument, so you could do this:
or
Also note that
writewill not append a newline to your output so if you need it you’ll have to supply it yourself.Aside:
Using string formatting would give you more control over your output, so for instance you could write (both of these are equivalent):
to get three spaces, with leading zeros for your integer value followed by a newline:
format() will be around for a long while, so it’s worth learning/knowing.