Skip to main content
Solved

Need help with scripted parameter. I want to be able to grab a file path, then modify it by adding double slash. My code runs in python ide but not in FME. Thanks for your help!


fme
Contributor
Forum|alt.badge.img+7
  • Contributor

def add_double_slash(file_path):

  modified_path = ''

  for char in file_path:

    if char == '\\\\':

      modified_path += '\\\\\\\\'

    else:

      modified_path += char

  return modified_path

 

# Example usage

file_path = r'C:\\Users\\Username\\Documents\\file.txt'

modified_file_path = add_double_slash(file_path)

print(modified_file_path)

Best answer by david_r

I'm not quite sure I understand what you want, but if you want a scripted private parameter to add double backslashes to a public parameter containing a filename, you could use the following code in your Python scripted parameter:

import fme
file_path = fme.macroValues.get('FILE_PATH''')
return file_path.replace('\\''\\\\')

Be aware that there are no checks on whether FILE_PATH already contains double backslashes.

imageimageimage

View original
Did this help you find an answer to your question?

4 replies

david_r
Celebrity
  • Best Answer
  • June 14, 2023

I'm not quite sure I understand what you want, but if you want a scripted private parameter to add double backslashes to a public parameter containing a filename, you could use the following code in your Python scripted parameter:

import fme
file_path = fme.macroValues.get('FILE_PATH''')
return file_path.replace('\\''\\\\')

Be aware that there are no checks on whether FILE_PATH already contains double backslashes.

imageimageimage


fme
Contributor
Forum|alt.badge.img+7
  • Author
  • Contributor
  • June 14, 2023
david_r wrote:

I'm not quite sure I understand what you want, but if you want a scripted private parameter to add double backslashes to a public parameter containing a filename, you could use the following code in your Python scripted parameter:

import fme
file_path = fme.macroValues.get('FILE_PATH''')
return file_path.replace('\\''\\\\')

Be aware that there are no checks on whether FILE_PATH already contains double backslashes.

imageimageimage

@david_r​  Yes, i want to grab the file path from a public parameter(C:\Users\Username\Documents\file.txt'), modify it through the scripted parameter (Private) by adding double slashes C:\\Users\\Username\\Documents\\file.txt to be used as  value in the transformer PDF2TEXTREADER


david_r
Celebrity
  • June 14, 2023
fme wrote:

@david_r​  Yes, i want to grab the file path from a public parameter(C:\\Users\\Username\\Documents\\file.txt'), modify it through the scripted parameter (Private) by adding double slashes C:\\\\Users\\\\Username\\\\Documents\\\\file.txt to be used as value in the transformer PDF2TEXTREADER

You could also simply drop the Python code and use a ParameterFetcher to retrieve the path, then a StringReplacer to replace \\ with \\\\ before sending it to the PDF2TEXTREADER.


fme
Contributor
Forum|alt.badge.img+7
  • Author
  • Contributor
  • June 14, 2023

Thank you, @david_r​ !


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings