Skip to main content
Question

Find empty folders


fikusas
Contributor
Forum|alt.badge.img+5

I need to find all empty folders. Any hidden system files do not count (for example, Thumbs.db). I tried using Python, but unsuccessfully. Can someone help me on this?

import fme
import fmeobjects
import os

if len(os.listdir(feature.getAttribute('path_directory_windows)') ) == 0:
    feature.setAttribute('Value'0)
else:    
    feature.setAttribute('Value'1)

8 replies

ebygomm
Influencer
Forum|alt.badge.img+32
  • Influencer
  • April 24, 2020

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


david_r
Celebrity
  • April 24, 2020

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".


fikusas
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • April 24, 2020
david_r wrote:

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

 


ebygomm
Influencer
Forum|alt.badge.img+32
  • Influencer
  • April 24, 2020
fikusas wrote:

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:

fikusas
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • April 24, 2020
ebygomm wrote:

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.

0684Q00000ArN7zQAF.png


david_r
Celebrity
  • April 24, 2020

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 = [f 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)

david_r
Celebrity
  • April 24, 2020
ebygomm wrote:

There's an errant bracket

swap line 5 with this

if len(os.listdir(feature.getAttribute('path_windows')))==0:

Thanks! Fixed.


fikusas
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • April 24, 2020
david_r wrote:

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 = [f 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!


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