Python builtin open() expects int in dynamo!?

Hey,
I came across a strange error just moments ago, either it’s just me and my colleagues collectivly missing the point (by miles!), or there is a fundamental error in writing files with python in dynamo. I know what I think is the likelier scenario…

Anyway,
I just want to do a simple file = open(filepath, mode) to log some data to a .txt document on my drive every time the script is run.

these are the lines of code thats supposed to do the work, get_data() beeing the function that collects the data i want to log and formats it into a string.

log_dir = "C:\\testlog"
filename = "testlog.txt"
filepath = log_dir + '\\' + filename
filepath = str(filepath)

file = open(filepath, "a")
file.write('\n')
file.write(get_data())
file.close()

When running i get:
image

line 52 beeing: file = open(filepath, "a").

I find no information that ironpython should be any different than python 2.7 or 3.6 in regards to open() or any information about this particular issue in Dynamo, in fact I’ve seen examples doing the exact same thing as above posted that seems to work for other. Also the script works without any errors when I run it in PyCharm with python 2.7.13 interpreter.

Very greatful for any tips as to what’s wrong here!!

Best

/Jakob

There seems to be something else going on. It works fine for me.

For some reason that I can’t explain it now seems to work, only thing I’ve done is to re-copy-paste the code from PyCharm into another python node and remove import calendar from my imports list.

Some problems however remain. After creating, appending and closing the file, the file seem to remain open when I attempt to delete it manually. it seems file.close() does not close the file properly when called from within dynamo, while the same code in PyCharm closes the file and causes no further issues. Is there a way to release my file from Revit from within the script?

You may have to close the Dynamo or even Revit session as well. It could be keeping the data stored in the local session.

1 Like