5

I'm trying to add a matrix to an existing csv file. Following this link, I wrote the following code,

f_handle = file(outfile+'.x.betas','a')
np.savetxt(f_handle,dataPoint)
f_handle.close()

where I have imported numpy as np, i.e.

import numpy as np

But I get this error:

f_handle = file(outfile+'.x.betas','a')
TypeError: 'str' object is not callable

I can't figure out what the problem seems to be. Please help :)

1
  • p.s. outfile is a string Commented Jul 18, 2013 at 18:40

2 Answers 2

12

It looks like you might have defined a variable named file which is a string. Python then complains that str objects are not callable when it encounters

file(...)

You can avoid the issue by, as Bitwise says, changing file to open.

You could also avoid the problem by not naming a variable file.

Nowadays, the best way to open a file is by using a with-statement:

with open(outfile+'.x.betas','a') as f_handle:
    np.savetxt(f_handle,dataPoint)

This guarantees that the file is closed when Python leaves the with-suite.

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

Comments

2

Change file() to open(), that should solve it.

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.