Skip to main content
Question

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

  • August 25, 2016
  • 1 reply
  • 15 views

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)

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

1 reply

takashi
Celebrity
  • August 25, 2016

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.