0

Here is my code:

f = open("text.txt", "w+")
var2 = input()
f.write(var2'\n')

How can I make this work?

jude
  • 9
  • 2
  • Possible duplicate of [Writing string to a file on a new line every time](https://stackoverflow.com/questions/2918362/writing-string-to-a-file-on-a-new-line-every-time) – Eli Korvigo Jun 17 '18 at 08:16
  • Welcome to SO. Before asking beginner-level questions, you should search for existing solutions. – Eli Korvigo Jun 17 '18 at 08:18

2 Answers2

-1

This should work

f = open("text.txt", "w+")
var2 = input()
f.write(var2 + '\n')
f.close()

But, this is a better way to do it

with open("text.txt", "w+") as f:
    f.write(var2 + '\n')
Sunitha
  • 11,777
  • 2
  • 20
  • 23
-1

if var is a string, you can just do f.write(var + '\n'). If var is not a string, you will need to do f.write(str(var) + '\n')

John Anderson
  • 35,991
  • 4
  • 13
  • 36