Question

Is the way I implement fme feature setattribute correct for my purpose?

  • 25 August 2016
  • 1 reply
  • 1 view

Hi everyone,

I modified the FilePropertyExtractor created by Takashi Iijima to suite my needs but it doesnt seems to store the attribute value correctly.

The Python code in PythonCaller below is to get multiple file from a folder and get the file's creation date (modified, etc.....) 

It runs without error. Since the files in my folder was of different creation dates, I expect the output  will follow accordingly as well but somehow only one date was shown for every outputs rows.

The print statement does show different value though.

I suspect that I must have missed the part that ensure the values are properly stored and returned.

Pls help me to correct it.

# Original Implementation by Takashi Iijima, 2016-05-25
# This a modified version

import fme, fmeobjects
import os
def extractFileProperty(feature):
    try:
        path = feature.getAttribute('__filepropertyextractor.path')
        for file in os.listdir(path):
            fullFileName=os.path.join(path,file)
            
            if os.path.exists(fullFileName) and not os.path.islink(fullFileName):
                s = os.stat(fullFileName)
                feature.setAttribute('_file_type', 'File' if os.path.isfile(fullFileName) else 'Directory')
                'feature.setAttribute('_file_size', s.st_size)
                'feature.setAttribute('_file_atime', int(s.st_atime))
                'feature.setAttribute('_file_mtime', int(s.st_mtime))
                feature.setAttribute('_file_ctime', int(s.st_ctime))
                print (int(s.st_ctime))
                
            else:
                raise Exception('specified file/directory does not exist')
    except Exception as ex:
        feature.setAttribute('__filepropertyextractor.error', '%s' % ex)


1 reply

Userlevel 2
Badge +17

Hi @hunphenglim, if you need to extract file properties for each file under a specified directory, you will have to define a class rather than a function. This is an example (error handling is omitted).

import os
class FilePropertyExtractor(object):
    def input(self, feature):
        dir = feature.getAttribute('__filepropertyextractor.path')
        if os.path.exists(dir) and os.path.isdir(dir) and not os.path.islink(dir):
            for file in os.listdir(dir):
                path = os.path.join(dir, file)
                if not os.path.islink(path):
                    s = os.stat(path)
                    feature.setAttribute('_file_name', file)
                    feature.setAttribute('_file_type', 'File' if os.path.isfile(path) else 'Directory')
                    feature.setAttribute('_file_size', s.st_size)
                    feature.setAttribute('_file_atime', int(s.st_atime))
                    feature.setAttribute('_file_mtime', int(s.st_mtime))
                    feature.setAttribute('_file_ctime', int(s.st_ctime))
                    self.pyoutput(feature)

However, it might be easier to use the Directory and File Pathnames (PATH) reader with setting 'YES' to the 'Retrieve file properties' parameter in this case.

Reply