Create filepath String with '\R'

I am trying to create a dynamic file path by creating a string for the file path. To test I simply created a label and want to make sure I have the formatting correct but I keep getting errors related to \R and \W being in the string.

The filepath I am trying to create is:
C:\Recipe In\WG10-CSV.csv.
I am going to add a dynamic value after Recipe In.

I have tried creating the string by separating the \ in the expression but no luck. I keep getting an error. How would I go about creating this filepath String?

change \ to / idk

Are you creating this filepath in a script? If so, double backslashes will work. In some cases forward slashes also work.

import csv
with open("C:\\Recipe In\\WG10-CSV.csv") as wg10:
    csvLines = csv.reader(wg10)
    for line in csvLines:
        # do some stuff
        pass

OR

import csv
with open("C:/Recipe In/WG10-CSV.csv") as wg10:
    csvLines = csv.reader(wg10)
    for line in csvLines:
        # do some stuff
        pass

The reason this is a problem for you is that \ is the escape character for python strings. \n is how you represent a newline, \t is tab. There are many others.

3 Likes

I did figure that about the \r and \w. Would the path still work if the the slashes are doubled and backwards?

I am trying to create the filepath in a string. I have the code below but that doesn’t seem to work either. I want filepath to be a string that I feed into the fileExists function. Getting an error with that as well.

		filepath = 'C:\Recipe In\BT{}\WG10-CSV.csv'.format(furnace_brass_tag_num)).value
		logger.info(toStr(filepath))
		if system.file.fileExists(toStr(filepath)):
#		if system.file.fileExists("C:\Recipe In\WG10-CSV.csv"):

format is pyhton 3…

Try:
filepath = 'C:\\Recipe In\\BT%s\\WG10-CSV.csv' %(furnace_brass_tag_num)

'hello {name}'.format(name="bob") definitely works in jython 2.7

oh? i thought it was python 3+

edit: seems its 2.6
and fstring format is 3.6

Seems like python 3 is all about f-strings, which are super convenient and would be a great addition to Ignition, but I don’t think jython is going to catch up any time soon.

1 Like

Yes you definitely need \\ if you are using backslashes. If your directory had a folder starting with a lowercase n then "C:\new Folder" I think would be interpreted as C: then a newline (because \n denotes new line), then "ew Folder". Does double backslashes solve your problem?

1 Like

I did get label to come up with correct syntax by using double \.

Still can’t get the script to work.

I have the following lines of code and I am getting this error:

	elif state == 10 and command == 'upload_recipe':
		# Look for recipe .csv file, set part/operation, get setpoints from database, and update state
		filepath = 'C:\\Recipe In\\BT{}\\WG10-CSV.csv'.format(furnace_brass_tag_num).value
#		logger.info(toString(filepath))
		if system.file.fileExists(filepath):

I get the following error on the logger:
Error running action ‘dom.onKeyUp’ on Templates/Embedded/RecipeView@C$0:1/root/Scanner_Container/OperationID/Field: Traceback (most recent call last): File “function:runAction”, line 7, in runAction File “module:RecipeControl”, line 59, in update_recipe_state_machine NameError: global name ‘toStr’ is not defined

Line 59 is the one where I am setting filepath

furnace_brass_tag_num.value perhaps?

filepath = 'C:\\Recipe In\\BT{}\\WG10-CSV.csv'.format(furnace_brass_tag_num.value)

Correction…this is the error I am getting:

Error running action ‘dom.onKeyUp’ on Templates/Embedded/RecipeView@C$0:1/root/Scanner_Container/OperationID/Field: Traceback (most recent call last): File “function:runAction”, line 7, in runAction File “module:RecipeControl”, line 59, in update_recipe_state_machine AttributeError: ‘str’ object has no attribute ‘value’

I am assuming this is related to the furnace_brass_tag_num value.

I have this same formatting in other places in the script related to reading and writing to tags so not sure why it isn’t working here

Tried that change and get this error now:

Error running action ‘dom.onKeyUp’ on Templates/Embedded/RecipeView@C$0:1/root/Scanner_Container/OperationID/Field: Traceback (most recent call last): File “function:runAction”, line 7, in runAction File “module:RecipeControl”, line 59, in update_recipe_state_machine AttributeError: ‘unicode’ object has no attribute ‘value’

filepath = 'C:\\Recipe In\\BT{}\\WG10-CSV.csv'.format(furnace_brass_tag_num.value)

Where / How is furnace_brass_tag_num being set?

It is being passed into the script for the overall function:

def update_recipe_state_machine(furnace_brass_tag_num, command=None):
	"""Process state machine updates and trigger control functions.
	
	Params:
		furnace_brass_tag_num (str): Brass tag number of furnace
		command (str): Command to run against recipe state machine
	"""

I am using the same parameter elsewhere. Here is an example.

running = system.tag.read('[BladesideHT]BS/FurnaceBT{}/Machine Running'.format(furnace_brass_tag_num)).value

its just furnace_brass_tag_num then
so

C:\Recipe In\BT{}\WG10-CSV.csv’.format(furnace_brass_tag_num)

1 Like

Ok, if it’s already coming in as a value, then you shouldn’t need .value

The other example is from a tag read, so you need to get the value from the tag’s QualifiedValue

2 Likes

Awesome, thanks all! Got it to work by changing code to:

	elif state == 10 and command == 'upload_recipe':
		# Look for recipe .csv file, set part/operation, get setpoints from database, and update state
		filepath = 'C:\\Recipe In\\BT{}\\WG10-CSV.csv'.format(furnace_brass_tag_num)
#		logger.info(toString(filepath))
		if system.file.fileExists(filepath):
1 Like

nice^^ mark the topic solved :wink:

1 Like

Done! Didn’t realize that, I should go back and mark some of my other posts solved too

1 Like