f = open('foo.txt','w')
print('blah',123,file=f)
print('blah',123,file=f)
f.close()
g = open('goo.txt','w')
g.write('blah'+str(123))
g.write('blah'+str(123))
g.close()
Just be aware that .write() only accepts a single argument, and does not tack a newline at the end.
cat foo.txt:
blah 123
blah 123
cat goo.txt:
blah123blah123
If you want a newline, you need explicitly add it yourself:
g = open('goo2.txt','w')
g.write('blah'+str(123)+'\n')
g.write('blah'+str(123)+'\n')
g.close()
No need to close the files: "with" takes care of that for you.
with open('foo.txt') as f, open('goo3.txt','w') as g:
# read and write to these files
g.write('blah'+str(123)+'\n')
g.write('blah'+str(123)+'\n')