system.file.writeFile | IOError: Unable to create path to file

I swear I've done this before but I can't find my code snippet...

I'm getting an IO error and having a heck of a time debugging it. The Ignition service is running as 'Local System' so permissions should not be an issue.

My code is system.file.writeFile("C:\\foo.txt", "bar"). I have tried single forward slash, etc. I don't know where to look to get more feedback. Running this in the script console (in read/write mode) or invoking a project script both results in:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File"<module:sandbox>", line 6, in testWriteFile
IOError: Unable to create path to file 'C:\foo.txt'

I've also tried supplying more arguments... system.file.writeFile("C:\foo.txt", "bar", False, "UTF-8" ), nothing is giving me any different results.

I think you need to be an admin to make a folder on the root of the C:\ drive so I don't think that just a regular local user is sufficient but instead would require the user to be admin. What happens if it tries making a folder inside C:\Users\TheLocalUserAccount\SmapleFolder\sampleFile.txt?

Also, the service is local system so that is what your gateway is running the script as. But if you're running this in script console, you are running it as whatever user you are logged in as. This does lead to some confusion (it's happened to me before) where things work in a gateway message handler but not in a script console or vice versa depending on your permissions.

Ah, okay. Didn't know that. Tested that same line triggered by a button click from a perspective page and it worked! Thank you!!

1 Like

Not just as the user you are logged in as, but on the machine that is running the Designer as well.

1 Like

So (on Windows) %USERDOMAIN%/%COMPUTERNAME%/%USERNAME% would need proper permissions for it to work from the script console; do I have that right?


I'm not understanding the possible implications of the client machine being used. Would probably be a good thing to keep in mind.

I'm not sure about the permissions stuff, but the main implication is that you could be running the Designer anywhere, so instead of writing to C:\\foo.txt on the Gateway, it's writing to C:\\foo.txt on whatever computer the Designer is running on.

Oooh! Yes, confirmed. My designer is connected to a remote gateway. The real file path I was using wouldn't make sense for the Windows environment on my laptop. I changed the path to (the literal equivalent of) %USERPROFILE%\Downloads and no doubt, it ran successfully from script console and created a file on my machine.

Thanks @Kevin.Herron!