Skip to main content
Question

Open a file on FME_SHARED_RESOURCE_DATA Directory with pythonCaller

  • November 11, 2021
  • 9 replies
  • 119 views

geobiker
Contributor
Forum|alt.badge.img

Hello,

 

I want to open a file with the PythonCaller stored on the [FME_SHARERESOURCE_DATA] directory.

The python enviroment can access the directory and the script end with an exception:

TypeError: expected str, bytes or os.PathLike object, not NoneType

 

Is it possible to map the FME_SHARED_RESOURCE_DATA Directory in the python script?

 

Kind regards

Stefan

9 replies

virtualcitymatt
Celebrity
Forum|alt.badge.img+35

Not sure it this really helps you, but I have found adding a ParameterFetcher before the a python caller can make things a little easier to work with. Get the [FME_SHARERESOURCE_DATA]  as an attribute.

Keep in mind that When working in Desktop the [FME_SHARERESOURCE_DATA]  will be empty because this us only used in FME Server.


geobiker
Contributor
Forum|alt.badge.img
  • Author
  • Contributor
  • November 11, 2021

My Problem is related to the server not to desktop. In the workbench i can read the directory on [FME_SHARERESOURCE_DATA] with the "Directory and File Pathnames Reader" successfully and then I can analyse the lasted filename as a attribut. But then the access the file in the python script is not possible.


david_r
Celebrity
  • November 11, 2021

You can access the parameter value in Python like this:

fmeserver_data = fme.macroValues.get('FME_SHAREDRESOURCE_DATA')
if fmeserver_data:
    # Note that FME_SHAREDRESOURCE_DATA is usually undefined on Desktop
    with open(fmeserver_data+'/filename.txt''r'as f:
        contents = f.readlines()

 


geobiker
Contributor
Forum|alt.badge.img
  • Author
  • Contributor
  • November 11, 2021

thanks.

Why it is not possible to use the path_unix attribut directly?

#  attribute from the Directory and File Pathnames Reader
feature.getAttribute('path_unix'

 

 

 


david_r
Celebrity
  • November 11, 2021
geobiker wrote:

thanks.

Why it is not possible to use the path_unix attribut directly?

#  attribute from the Directory and File Pathnames Reader
feature.getAttribute('path_unix'

 

 

 

Is it not? That is surprising. What is the value of 'path_unix'? What is the error message?


geobiker
Contributor
Forum|alt.badge.img
  • Author
  • Contributor
  • November 11, 2021
            json_file_new = feature.getAttribute('path_unix')
            logger.logMessageString("FME_SHAREDRESOURCE_DATA: %s"%(shared_resource_data_path))
            logger.logMessageString("PATH_UNIX: %s"%(json_file_new))
            json_data_new = codecs.open(json_file_new,encoding="UTF-8",mode="r")

LOG-FILE

279	2021-11-11 12:18:56 | FME_SHAREDRESOURCE_DATA: /data/fmeserver/resources/data/
280	2021-11-11 12:18:56 | PATH_UNIX: /data/fmeserver/resources/data/xxx/xxxx/xxxxxx/xxxxxxxx/yyyyyyyyy_20211111.json
281	2021-11-11 12:18:57 | Python Exception <TypeError>: expected str, bytes or os.PathLike object, not NoneType
282	2021-11-11 12:18:57 | Traceback (most recent call last):
283	2021-11-11 12:18:57 | File "<string>", line 37, in input
284	2021-11-11 12:18:57 | File "/usr/lib/python3.6/codecs.py", line 897, in open
285	2021-11-11 12:18:57 | file = builtins.open(filename, mode, buffering)
286	2021-11-11 12:18:57 | TypeError: expected str, bytes or os.PathLike object, not NoneType
287	2021-11-11 12:18:57 |
288	2021-11-11 12:18:57 | Error encountered while calling method `input'
289	2021-11-11 12:18:57 | PythonCaller (PythonFactory): PythonFactory failed to process feature

FME Version

FME Server 2020.2.1

Build 20806 - linux-x64


david_r
Celebrity
  • November 11, 2021
geobiker wrote:
            json_file_new = feature.getAttribute('path_unix')
            logger.logMessageString("FME_SHAREDRESOURCE_DATA: %s"%(shared_resource_data_path))
            logger.logMessageString("PATH_UNIX: %s"%(json_file_new))
            json_data_new = codecs.open(json_file_new,encoding="UTF-8",mode="r")

LOG-FILE

279	2021-11-11 12:18:56 | FME_SHAREDRESOURCE_DATA: /data/fmeserver/resources/data/
280	2021-11-11 12:18:56 | PATH_UNIX: /data/fmeserver/resources/data/xxx/xxxx/xxxxxx/xxxxxxxx/yyyyyyyyy_20211111.json
281	2021-11-11 12:18:57 | Python Exception <TypeError>: expected str, bytes or os.PathLike object, not NoneType
282	2021-11-11 12:18:57 | Traceback (most recent call last):
283	2021-11-11 12:18:57 | File "<string>", line 37, in input
284	2021-11-11 12:18:57 | File "/usr/lib/python3.6/codecs.py", line 897, in open
285	2021-11-11 12:18:57 | file = builtins.open(filename, mode, buffering)
286	2021-11-11 12:18:57 | TypeError: expected str, bytes or os.PathLike object, not NoneType
287	2021-11-11 12:18:57 |
288	2021-11-11 12:18:57 | Error encountered while calling method `input'
289	2021-11-11 12:18:57 | PythonCaller (PythonFactory): PythonFactory failed to process feature

FME Version

FME Server 2020.2.1

Build 20806 - linux-x64

Weird. What happens if you hard-code the filename into the call to codecs.open()? Does that work?

Can you also try to log the following to check what the object type of json_file_new is:

logger.logMessageString("type of json_file_new: %s" % type(json_file_new))

 

 


geobiker
Contributor
Forum|alt.badge.img
  • Author
  • Contributor
  • November 11, 2021

Hi david,

 

i found the problem. The second attribut for the second file was not set successfully. Now the PythonCaller works fine.

 

Thanks for your help

 

Kind regards

Stefan


david_r
Celebrity
  • November 11, 2021
geobiker wrote:

Hi david,

 

i found the problem. The second attribut for the second file was not set successfully. Now the PythonCaller works fine.

 

Thanks for your help

 

Kind regards

Stefan

Thanks, glad to hear you got it working.


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