Skip to main content
Solved

Using the TempPathnameCreator output in a PythonCaller to write files

  • July 5, 2022
  • 3 replies
  • 66 views

birgit
Influencer
Forum|alt.badge.img+21

Hey everyone,

 

I am trying to write a file to a temporary filepath in my PythonCaller script and I thought that using the TempPathnameCreator transformer might save me some time coding something which is a basic function in FME. Unfortunately if I try to write the file to the location I get a python error that says that the location does not exist. I have used the OS python package to test if the temporary path is valid and it is not. Am I using the TempPathnameCreator incorrectly and is it logical that my PythonCaller is unable to write to the temporary location?

Best answer by david_r

Hey David, thanks for your answer I also found this option myself. However my question is more on why the TempPathnameCreator and the PythonCaller are unable to work together.

The TempPathnameCreator only reserves the name of a temporary directory that is guaranteed to be unique, it doesn't actually create the directory for you. It can behave this way since practically all functionality in FME that writes a file will automatically recursively create any needed (sub-)directories.

This is not the case in Python, where you'll have to explicitly create all directories before trying to access them, e.g. using 

import os
os.makedirs(feature.getAttribute('_pathname'))

The Python function tempfile.TemporaryDirectory() will not only reserve the directory name, but it will also create the directory for you.

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

3 replies

david_r
Celebrity
  • 8391 replies
  • July 5, 2022

Why not use the Python tempfile module: https://docs.python.org/3/library/tempfile.html

It's very easy:

import tempfile
temp_dir = tempfile.TemporaryDirectory()
# Manipulate the directory contents here

 


birgit
Influencer
Forum|alt.badge.img+21
  • Author
  • Influencer
  • 131 replies
  • July 5, 2022

Why not use the Python tempfile module: https://docs.python.org/3/library/tempfile.html

It's very easy:

import tempfile
temp_dir = tempfile.TemporaryDirectory()
# Manipulate the directory contents here

 

Hey David, thanks for your answer I also found this option myself. However my question is more on why the TempPathnameCreator and the PythonCaller are unable to work together.


david_r
Celebrity
  • 8391 replies
  • Best Answer
  • July 5, 2022

Hey David, thanks for your answer I also found this option myself. However my question is more on why the TempPathnameCreator and the PythonCaller are unable to work together.

The TempPathnameCreator only reserves the name of a temporary directory that is guaranteed to be unique, it doesn't actually create the directory for you. It can behave this way since practically all functionality in FME that writes a file will automatically recursively create any needed (sub-)directories.

This is not the case in Python, where you'll have to explicitly create all directories before trying to access them, e.g. using 

import os
os.makedirs(feature.getAttribute('_pathname'))

The Python function tempfile.TemporaryDirectory() will not only reserve the directory name, but it will also create the directory for you.