Create filepath String with '\R'

Great info here. I also go this work as follows:

filePath = r"C:\Users\user\Documents\Project\MyFile.csv"

The r in front denotes a raw, (almost) un-escaped string Which is allowed in ignition and jython(python2.7)

That said, I wanted to add a component to my perspective view. And if I try to use some thing like this

csvPath = rstr(self.getSibling("FilePath").props.text)
or do the above without the r then try to update it as follows
csvPath = rcsvPath

I will get the following similar errors for both examples:

NameError: global name 'rstr' is not defined

With that said. I tried a few different ways like above, but can not seem to figure out a way to do this through a component. I was curious is this possible at all, or will we just need to change the file path manually in the code if we ever need to change it?

You can only create raw literals as in your first example. Try using repr().

csvPath = repr(self.getSibling("FilePath").props.text)

Unfortunately that does not work as well.

I get this error with this code:
Code:

csvPath = repr(self.getSibling("TextField").props.text)

Error:

IOError: [Errno 2] No such file or directory: 'u'"C:\\Users\\user\\Documents\\Project\\IgnitionFiles\\Work\\Alarms\\myfile.csv"''

Also tried this:
Code:

csvPath = eval(repr(self.getSibling("TextField").props.text))

Error:

IOError: [Errno 2] No such file or directory: u'"C:\Users\user\Documents\Project\IgnitionFiles\Work\Alarms\myfile.csv"'

You likely don't need any of these things.

If you've got a path that looks correct (visually, in the text field) then you should be able to pass it directly to the file function you want. You only need backslash escaping, raw strings, etc, when you are defining the string in your code. A string is a string is a string - all hints like the r prefix do is tell the local parser that the next encountered string should be parsed as a raw string - after parsing, it's a string like any other, no matter what values it contains.

1 Like

Hmmm not sure why it doesn't take effect then.

I tried it just with the component, no extra functions included, and get this error:

IOError: [Errno 2] No such file or directory: u'"C:\Users\User\Documents\Project\IgnitionFiles\Work\Alarms\myfile.csv"'

I think I'll leave it hard coded for now using the r"C:\FilePath.csv. It works fine but would have been nice for others to use a components with.

If you're in a Perspective context, keep in mind the script is running as whatever the Gateway service user is. Windows might be "lying" to the service and saying there's no such file because of permissions.

2 Likes

That would make sense. Thank you so much!

EDIT

I got this to work but not in the original way I was trying to do it.

The way I was try was to use that component value with the csv module syntax open with(path,"r") as f:
Trying to do it like that I would get errors.

BUT

If you use that component value and then use it in an Ignition function it works just fine. Wanted to export a dataset to a CSV file and the file path was put into a component(No quotes) and it worked just fine.