[Tutor] adding more text to a file

Hugo Arts hugo.yoshi at gmail.com
Sun Jan 17 18:52:33 CET 2010


On Sun, Jan 17, 2010 at 6:40 PM, Shizuha Aki <shizuhaaki1 at yahoo.com> wrote:
> Hi,
>
> I am new to python, and i am trying to make a program that makes like a txt
> file database. so i have my whole program in a while True loop, it asks for
> information about the person and then writes the information about the
> person to a txt file, but i need it to be able to add more entries instead
> of just one.
>
> my code is like this:
>
> while True:
>        name = raw_input("what is the name ")
>        age = raw_input("what is the age ")
>
>        out_file = open("persons.txt", "w")
>        out_file.write("name: ")
>        out_file.write(name)
>        out_file.write("\n")
>        out_file.write("age: ")
>        out_file.write(age)
>        out_file.write("\n")
>        out_file.write("\n")
>        out_file.close
>
> now i want to add like say 5 persons to the txt file, but whenever i enter a
> new person it just overwrites the previous, im sure its because of the "w"
> but how exactly do i need to change my code to add more text instead of
> overwriting?
>

you should check the python documentation for the 'open' function.
help() is your friend, works on any interactive session. On linux, you
could also check out the pydoc program. Or you could check the website
docs.python.org.

Hugo


More information about the Tutor mailing list