0

The section of coding I've written is as such:

thing=9
text_file=open("something.txt", "a")
text_file.write("\n", str(thing))
text_file.close()

This always returns the error Type error: "write" only takes 1 argument. 2 given. What I'm trying to do is that each time I run this code it writes on a new line rather than the same line. Right now, if this doesn't work, I'm a bit confused how to do this. Any help would be appreciated!

  • DUPLICATED: http://stackoverflow.com/questions/12377473/python-write-versus-writelines-and-concatenated-strings – boctulus Apr 20 '14 at 17:09

2 Answers2

0

The python interpreter is correct in saying:

"write" only takes 1 argument. 2 given

Python's file methods are documented here.

All you need to be doing is concatenating your string with the newline character. You can do so by replacing:

text_file.write("\n", str(thing))

with:

text_file.write("\n" + str(thing))

This will write an empty line before writing out what you want. This might not be what you are looking for. Instead you can do:

text_file.write(str(thing) + '\n')
shaktimaan
  • 11,962
  • 2
  • 29
  • 33
0

Add a newline to the end1 of the string with the + operator:

text_file.write(str(thing) + "\n")

1Note: If you add it to the front, you will get a blank line at the top of your file, which may not be what you want.