# Python Bridge Issue

**URL:** <https://mworks.discourse.group/t/python-bridge-issue/320>\
**Category:** Support\
**Created:** [July 5, 2018, 5:15pm UTC](https://mworks.discourse.group/t/python-bridge-issue/320 "2018-07-05T17:15:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 5, 2018, 5:15pm UTC](https://mworks.discourse.group/t/python-bridge-issue/320/1 "2018-07-05T17:15:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 5, 2018, 6:53pm UTC](https://mworks.discourse.group/t/python-bridge-issue/320/2 "2018-07-05T18:53:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 5, 2018, 8:44pm UTC](https://mworks.discourse.group/t/python-bridge-issue/320/3 "2018-07-05T20:44:34Z")

</div>

Hi Chris,

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

Best,  
Yvonne

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 6, 2018, 2:24pm UTC](https://mworks.discourse.group/t/python-bridge-issue/320/4 "2018-07-06T14:24:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 19, 2022, 9:44pm UTC](https://mworks.discourse.group/t/python-bridge-issue/320/5 "2022-07-19T21:44:27Z")

</div>

Closed on July 19, 2022, 9:44pm UTC


