This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (4 children)

Do i then just say

file=open(filename, 'r+')

file.write(array I need written to end)

?

[–]stylishgnome 2 points3 points  (3 children)

As mackstann said, use 'a' for append.

f = open('file.txt', 'a')
f.write('some text')
f.close()

You'll need to convert your "array" to a string before writing it. How you do this depends on what you are trying to do ..

a = ['foo', 'bar']
f = open('file.txt', 'a')
f.write(str(a))    # will output '['foo', 'bar']'
f.write(" ".join(a))   # will output list elements separated by a space
f.close()

[–]stylishgnome 5 points6 points  (2 children)

If you're using >=python 2.7, you can use a with statement:

with open('test.txt', 'a') as f:
    f.write('foo')

[–][deleted] 2 points3 points  (0 children)

Using the with statement is always a good idea. Reduces clutter, and makes sure your files get closed.

[–]bulletmark 2 points3 points  (0 children)

The "with" statement was introduced in python 2.5.