Python Bridge Issue

Hi Chris,

We’re attempting to write a python bridge to write some variables to a file, but it won’t write the file when called through MWorks. In our messing around with it we have the following in Works to call it:

run_python_file(stop_on_failure = YES
path = ‘rrr.py’
)

and the following in the python file:

a = getvar(‘a’)
setvar(‘a’, a +10)

f = open(‘workfile.txt’, ‘a’)
f.write(‘\nHello’)
f.close
print 1212

The effect of this is that when MWorks is run, and reports the variable a, it adds 10 to the variable, meaning the python script was successfully run, but it doesn’t write to the file. When running python through the terminal, however, it writes “Hello” to a file just fine. Is there anything else that should go into a python bridge of this sort?

Thanks,
Yvonne

Hi Yvonne,

f.close

You haven’t actually called “close” here. You need to add some parentheses:

f.close()

See if that fixes it.

Cheers,
Chris

Hi Chris,

This fix doesn’t seem to change the behavior of the code at all.

Best,
Yvonne

Ok. I thought maybe the write hadn’t been flushed, but I guess that’s not the problem.

f = open(‘workfile.txt’, ‘a’)

Next guess: If you’re writing to a filename with a relative (or empty) path, then it will be found/created relative to the working directory of the experiment. This is an experiment-specific subdirectory of $TMPDIR/MWorks/Experiment Cache on the machine running MWServer. Since this is probably not where you actually want the file to be created, you need to include an absolute path in the filename, e.g.

f = open('/Users/yvonne/something/workfile.txt', 'a')

As an aside, this is why the create_whitenoise_images.py script I gave you needs imagepath to be absolute.

Sorry, I realize this is not at all obvious! But hopefully it’s the correct fix.

Cheers,
Chris