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?
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.
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"):
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.
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?
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
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
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â
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.
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):