Skip to main content
Solved

Using the TempPathnameCreator output in a PythonCaller to write files


birgit
Influencer
Forum|alt.badge.img+16

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

birgit wrote:

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.

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

3 replies

david_r
Celebrity
  • 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+16
  • Author
  • Influencer
  • July 5, 2022
david_r wrote:

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
  • Best Answer
  • July 5, 2022
birgit wrote:

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.


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