Save string to a text file

I am running ignition in a docker container, when I save a string to a text file, what folder does it save to?

Below is the code, right of the doc:

data = "Some text here"
filename = system.file.saveFile("MyTextFile.txt")
if filename is not None:
   system.file.writeFile(filename, data)

I can't find 'MyTextFile.txt' :roll_eyes:

Likely /usr/local/bin/ignition/ in the container. That's wouldn't be preserved with the normal volume options, but you should be able to enter the container while running to check.

You probably should make your folder path explicit.

I do this sort of thing all of the time, but I never use the saveFile method; system.file.writeFile is enough. Also, I typically specify a third argument, that identifies whether or not I want to append or overwrite in the event that the file already exists. Overwrite is probably the default, but when assembling a file from a blob or something of that nature, the third argument allows this to happen.

Here is an example that could be easily adapted for your usage case:

path = 'C:/Users/YOURUSERNAME/Desktop/MyTextFile.txt'	
data = "Some text here"
system.file.writeFile(path, data, False)

Apparently system.file.saveFile causes error.

I end up using below, this works and it's preserved

data = "My other text"
system.file.writeFile("/user-data/MyTextFile.txt", data)

Looks like you've got it sorted, so that is good. I'll mention that system.file.saveFile is only available in [Vision] Client scope since it prompts the user with a save dialog. In gateway scope, you've got to specify the path directly to writeFile().

2 Likes

I don't like resurrecting and old post, but I'd like to keep the thread going for someone else walking this path.

I'm attempting to do something similar. I'm running in a container, and I'd like to pull some blobs down from a database and save the files locally to allow source binds for things like images and audio files.

I get an error if I try something simple like

data = "Testing"
system.file.writeFile("test.txt", data)

If I give it a full path, I do not receive an error, but I never see the file. Something like

data = "Testing"
filename = "/usr/local/bin/ignition/test.txt"
system.file.writeFile("/usr/local/bin/ignition/test.txt", data)

I know I can pull the blob and get the file. This is just to prove out that I can save the file to the container in a persisted volume to allow access to perspective resources. I'm unsure if the path is relative to a location or starts at the root, but I haven't seen a difference in behavior when I try various approaches.

I'm sure someone else has solved this, but this is the only thread I have been able to find thus far. Any thoughts?

Are you running this in Client/Designer or Gateway scope? You want to be in gateway scope for the file to end up in your container.

Hey Kevin. The script was written at the gateway, but I was calling it from a script console session on a client as a quick test. This was me just not thinking through the execution. Thanks for the quick response.