Reputation:
I've attempted to create or append a text file if it already exists and I was able to do that after research. However I'm stuck in trying to figure out how to add information underneath old info.
For example I have a text file which contains sam10
.
name = input('Your name?')
squad = group
number = input('A number?')
target = open(squad, 'a')
target.write(name)
target.write(str(number))
If I ran this all this does it add on to the text text file and end up with a text file (called group) like so: sam10james5
.
How could I edit this code so that I end up with james5
beneath sam10
?
So I see it like this when I open the text file:
sam10
james5
Upvotes: 0
Views: 54
Reputation: 94
You are just missing adding a 'new line':
target.write(name)
target.write(str(number) + '\n')
write
will simply add the exact characters provided to the file, nothing more. You need to add the new lines as needed.
Upvotes: 2
Reputation: 1968
You only have to add a "\n"
:
Change:
target.write(name)
With:
target.write("\n"+name)
Upvotes: 2