Hamilton Python Script Not Writing to file


I’m posting this here rather than on the PyHamilton part of the forums due to the fact that I’m not actually using the PyHamilton software at the moment (due to work related IT issues). I have written a python script that reads a file, takes an input from that file, and then writes to another separate file. When I run the python script on its own it runs perfectly fine. Within the Hamilton Method Editor, I call the python script, which works fine, but when it runs the script, it seems to only be able to read files and not write to them. I’m assuming this is a permissions issue, but I’m having trouble finding out where to fix it; any help would be greatly appreciated.


Hi Tarun,

I’m doing something similar, so I don’t think it’s a permission issue. How do you figure that it actually reads the file? What exit code is returned to VENUS?


Hi Dominik,

I am not 100% certain that it is reading the file but I think its managing as it seems to read and split a string from a file, but then just doesn’t write to the csv that I want it to. The csv is meant to act as a check that a subsequent file-read is valid (essentially to check that the file I want it to read is of the right type). The aim is that it reads a file path, goes to check if the file a) exists and b) is of the right format, then writes to another file whether it is or isn’t valid, which is then read by Venus so that it gets the user to try put in a new input file rather than crashing with an error that “database or object is read only”, which I assume is because it’s trying to read it as though its a CSV when its being given a pdf/anything else.


Put a bunch of print statements (or logging outputs) in the python script so you can tell what line its getting to, and other data like current working directory. It could be hitting an error that isn’t getting sent back up to Venus.


This might be a stupid question but where do I view print statements when I call it through venus? Obviously when I run the script on its own I can see it in the shell, but I don’t know where it would print to when being called from Venus.


I dont know if there is any way. You may want to redirect the Python console output to a file

import sys
sys.stdout = open('/the/path/to/your/file', 'w')

At the end of the script call


For trouble shooting, you can also introduce message boxes into your Python scripts with tKinter. I find these to be useful for when testing various parts of the Python script.

You can also put a sys.exit(“THIS IS YOUR STRING”) into different places in your script, this will get sent back to VENUS and prompted in the Tracefile.

€: This is also the only way I found to get data from python back into VENUS, because you can bind the return value to a variable and then use it in VENUS.

Thanks to you all! In the end I managed to do the part I was having difficulty with without using python. In case anyone has a similar issue: instead I used StrFind from HSLStrLib and looked for a substring of the file extension with the variable, in my case “.csv”. Not bulletproof but good enough to prevent accidents for what I’ll be doing at least.

I had troubles knowing why the application in FluentControl fails as the window closes immediately. So, I added logging (python library) to the .exe that it writes an extra log file, so you can manage to traceback the error history or information you are printing. (log.info(“…”) instead of print(“…”)). Then you at least know whats going on. Also, you can store the return value of each application. A return value of 0 means it succeeded, and 1 means it failed.

Thats very useful to know for future problems that will inevitably arise - thank you!

1 Like

Another trick I’ve used for debugging scripts initiated through Venus is to sprinkle


in as pseudo break-points. You’ll be able to read all of the print statements up to that point as well because the input step blocks Venus from closing the window. Can help narrow down where your program is failing. Just make sure that your window isn’t set to be minimized in the shell command through Venus and you should be able to see it.

A neat trick there - I’ll definitely be adding that to my repertoire :smiley:

I had a similar problem, where the python script, when run through the command line, would successfully save the desired file, but did not manage to save the written file when executed through Venus. Specifying the full save path in the python script fixed the problem for me.