# Writing a python script that runs a processing program

**URL:** <https://discourse.processing.org/t/writing-a-python-script-that-runs-a-processing-program/16456>\
**Category:** Processing.py\
**Created:** [December 16, 2019, 8:32pm UTC](https://discourse.processing.org/t/writing-a-python-script-that-runs-a-processing-program/16456 "2019-12-16T20:32:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hinomupi](https://avatars.discourse-cdn.com/v4/letter/h/8dc957/32.png) [@hinomupi](https://discourse.processing.org/u/hinomupi)\
**Post date:** [December 16, 2019, 8:32pm UTC](https://discourse.processing.org/t/writing-a-python-script-that-runs-a-processing-program/16456/1 "2019-12-16T20:32:56Z")

</div>

I am trying to create a twitter bot that posts randomized content, similar to OreoBot, that uses processing. I have the processing script (made with python mode) that creates the image and I know how to post the image, now all I have to do is make the python script run the processing script and get a string of text from it when it finishes running, how do I do that?

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [December 16, 2019, 9:36pm UTC](https://discourse.processing.org/t/writing-a-python-script-that-runs-a-processing-program/16456/2 "2019-12-16T21:36:24Z")

</div>

Sorry, I’m not familiar with OreoBot. It sounds like you want to post an image and some text you’ve generated with Processing?

You can do this with the command-line version of Processing.py. You can download it here:  
[https://py.processing.org/tutorials/command-line/](https://py.processing.org/tutorials/command-line/)

You would add something like this to the end of your sketch file:

```auto
...
saveStrings('text.txt', ['blah blah blah'])
saveFrame('image.jpg')
exit()

```

If you run this sketch from the command line, Processing will save the string to a file named `text.txt`, and the image to a file named `image.jpg`:

```auto
java -jar processing-py.jar sketch_name.pyde

```

The `exit()` function closes the display window (which will probably appear and disappear before you can even see it, anyhow). \* _There’s probably a way to do this completely headless – i.e. no `exit()` function required – but I’m not sure how to do this with processing.py’s command-line version._

If the above is working fine, add the following code to your bot script, which calls the command-line version of Processing from Python, then waits for the image to be generated before proceeding:

```auto
import os
import time

os.system('java -jar processing-py.jar sketch_name.pyde')

while not os.path.exists('image.jpg'):
    time.sleep(1)

# do twitter stuff here ...

```

You’ll have to substitute the paths and filenames to work with your project. You can read in the text file using the standard Pyhton `with open(...` approach.

---

<div class="post-metadata">

**Author:** ![hinomupi](https://avatars.discourse-cdn.com/v4/letter/h/8dc957/32.png) [@hinomupi](https://discourse.processing.org/u/hinomupi)\
**Post date:** [December 17, 2019, 1:54pm UTC](https://discourse.processing.org/t/writing-a-python-script-that-runs-a-processing-program/16456/3 "2019-12-17T13:54:27Z")

</div>

Yes, I’m going to generate an image and some text using processing

I did that and it returned “Error: Unable to access jarfile processing-py.jar”

Edit: I don’t know where is processing-py.jar

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [December 17, 2019, 8:54pm UTC](https://discourse.processing.org/t/writing-a-python-script-that-runs-a-processing-program/16456/5 "2019-12-17T20:54:12Z")

</div>

Your file path(s) are wrong. There are different ways to fix this. This method might be the easiest to explain –

You’ve downloaded and extracted a folder containing the command-line version of processing.py. Maybe just place your python script and .pyde file in here.

```auto
processing.py-30__
│ ...
│ processing-py.jar
│ ...
│ python_script.py
│ sketch_name.py

```

It should work now (everything can be accessed using relative paths).
