Solved

Using the TempPathnameCreator output in a PythonCaller to write files


Badge +9

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?

icon

Best answer by david_r 5 July 2022, 16:02

View original

3 replies

Userlevel 4

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

 

Badge +9

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.

Userlevel 4

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.

Reply