0

I wanna save some string on a file that i called inv.data. Every time i write a special command, I want to save a string in the file. The string should be at the last line in the file all times. I read something about append, so I tried to do something like this:

#Open and close the inventroy file
fileOpen = open('inv.data', 'a')
fileOpen.write(argOne)
fileOpen.close()

fileOpen = open('inv.data', 'r')
savedData = fileOpen.read().splitlines()
fileOpen.close()

This works fine the first time I want to add something during runtime, but when I try to add the second string it looks something like this:

sword
axe
shield
bow
flower
monsterLol

Where monster was the first add, and Lol was the second thing I added. What am I missing? Do I need to specify that it should go to a new line each time or?

0

1 Answer 1

4

New line is not getting added and hence the next entry is appended in the same line. You ca rectify this as follows:

fileOpen.write(argOne + '\n')

This way you don't have to modify the way you input your arguments.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.