Skip to main content
Question

Python script with a parameter in a filename.


flying_squirrel
Contributor
Forum|alt.badge.img+5

Hello,

I have a script which is written like this:

import os

parent = os.path.abspath('..')

file= os.path.abspath('../file_08-06-2024.xls')

return file

I would like to replace the date with an attribute defined in a attributeCreator transformer which could change and i write this :

file=os.path.abspath('../file_$(date).xls')

When i run, it doesn't work. 

Can you help me ?

Thank you.

2 replies

birgit
Influencer
Forum|alt.badge.img+16
  • Influencer
  • June 11, 2024

Hi,

It looks like you are trying to apply python in an AttributeCreator which is impossible. To run python code you basically need to use the PythonCaller which is overkill for what you are trying to do. First you need to get an attribute with the absolute path you are trying to edit but I'm going to assume you don't have a problem with that. Next you put the following in the AttributeCreator: @Value(AbsolutePathAttribute)\file_$(MyDate).xls

  • Note that my parameter is named "MyDate” so you need to fix that to "date” for your situation.

See the screenshot for the setup:

Attribute Creator settings

flying_squirrel
Contributor
Forum|alt.badge.img+5

Hi,

My python script is written inside a private parameter. This parameter is linked to a WorkspaceRunner transformer.

This transformer searchs a XLS file which could change depending the date of the day. And this file is situated in a different file hierarchy (../file_$(date).xls)

I succedeed with this syntax using FME_MACRO_VALUES and this syntax of f-strings ( Python V3...)

=os.path.abspath(f'../file_{date}.xls')

It seems that $(date) isn't supported. It was a syntax used in Python V2...


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