Where is the path_directory_windows coming from? If you are using the Path Reader to just read in directories, the directory is actually stored in path_windows
In addition to what @ebygomm said, you'll also have to wrap the Python code into a small function, e.g.
import fmeobjects
import os
def processFeature(feature):
if len(os.listdir(feature.getAttribute('path_windows'))) == 0:
feature.setAttribute('Value', 0)
else:
feature.setAttribute('Value', 1)
In the PythonCaller, type "processFeature" in the setting for "Class of function to process features".
In addition to what @ebygomm said, you'll also have to wrap the Python code into a small function, e.g.
import fmeobjects
import os
def processFeature(feature):
if len(os.listdir(feature.getAttribute('path_windows'))) == 0:
feature.setAttribute('Value', 0)
else:
feature.setAttribute('Value', 1)
In the PythonCaller, type "processFeature" in the setting for "Class of function to process features".
Getting this error:
2020-04-24 11:50:06| 0.8| 0.3|ERROR |Python Exception <SyntaxError>: invalid syntax (<string>, line 6)
2020-04-24 11:50:06| 0.8| 0.0|FATAL |PythonCaller (PythonFactory): PythonFactory failed to process feature
Getting this error:
2020-04-24 11:50:06| 0.8| 0.3|ERROR |Python Exception <SyntaxError>: invalid syntax (<string>, line 6)
2020-04-24 11:50:06| 0.8| 0.0|FATAL |PythonCaller (PythonFactory): PythonFactory failed to process feature
There's an errant bracket
swap line 5 with this
if len(os.listdir(feature.getAttribute('path_windows')))==0:
There's an errant bracket
swap line 5 with this
if len(os.listdir(feature.getAttribute('path_windows')))==0:
Now it's working, but gives value '1' if folder contains any hidden system files. But actually folder is empty.
If you want to exclude all hidden and system files, you'll have to dig into OS-specific functions. On Windows that would mean installing and using the win32api Python modules, which aren't installed with FME. (Example here: https://stackoverflow.com/a/14063074)
If the number of hidden files is known and of reasonable length, then you can manually exclude them with a list of known filenames to ignore, like so:
import fmeobjects
import os
FILES_TO_IGNORE = ('THUMBS.DB', 'HIDDEN_FILE_NO1.TXT')
def processFeature(feature):
all_files = os.listdir(feature.getAttribute('path_windows'))
files = if for f in all_files if not f.upper() in FILES_TO_IGNORE]
if len(files) == 0:
feature.setAttribute('Value', 0)
else:
feature.setAttribute('Value', 1)
If you want to exclude all hidden and system files, you'll have to dig into OS-specific functions. On Windows that would mean installing and using the win32api Python modules, which aren't installed with FME. (Example here: https://stackoverflow.com/a/14063074)
If the number of hidden files is known and of reasonable length, then you can manually exclude them with a list of known filenames to ignore, like so:
import fmeobjects
import os
FILES_TO_IGNORE = ('THUMBS.DB', 'HIDDEN_FILE_NO1.TXT')
def processFeature(feature):
all_files = os.listdir(feature.getAttribute('path_windows'))
files = if for f in all_files if not f.upper() in FILES_TO_IGNORE]
if len(files) == 0:
feature.setAttribute('Value', 0)
else:
feature.setAttribute('Value', 1)
This works like a charm. Thanks!