[python] writing integer values to a file using out.write()

I am generating some numbers(lets say, num) and writing the numbers to output file using outf.write(num).
But compiler 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?

This question is related to python

The answer is


Also you can use f-string formatting to write integer to file

For appending use following code, for writing once replace 'a' with 'w'.

for i in s_list:
    with open('path_to_file','a') as file:
        file.write(f'{i}\n')

file.close()

i = Your_int_value

Write bytes value like this for example:

the_file.write(i.to_bytes(2,"little"))

Depend of you int value size and the bit order your prefer


any of these should work

outf.write("%s" % num)

outf.write(str(num))

print >> outf, num