I saw a great interface to FME with Python

What you guys are doing with it? I want ideas.


I'm just getting started with FME, and am using a shutdown script to copy my target FGDB to another location and to save out the log file:

import distutils.dir_util, shutil, os, time, locale

src = 'C:/Testing/FME/TPW/Third_Party_Wells.gdb'
dst = '//share/Data Services/GIS Data/Data/Third Party Wells/Third_Party_Wells.gdb'

distutils.dir_util.copy_tree(src, dst)

logfile = FME_LogFileName
shutil.copy(logfile, 'C:/temp/PRD_' + os.path.basename(logfile)[:-4] + '_' + time.strftime('%Y_%m_%d_%H_%M_%S', time.localtime()) + '.log')

# Get features written counts
shl_count = str(FME_FeaturesWritten['ThirdPartyWellsSurface'])
bhl_count = str(FME_FeaturesWritten['ThirdPartyWellsBottom'])
lat_count = str(FME_FeaturesWritten['ThirdPartyWellsLaterals'])

# Write out features written counts to log
fm_log = open('C:/temp/PRD_Counts.log','a')
fm_log.write(time.strftime('%m/%d/%Y %I:%M:%S', time.localtime()) + ',' + shl_count + ',' + bhl_count + ',' + lat_count + ',' + str(FME_TotalFeaturesWritten) + '\n')

That's pretty basic, but there really is no limit I don't think. There are tons of ideas here as well.

EDIT: Added in code to get numbers of features written and push those out to CSV log file.


Have a look at Oliver's Python Corner. There's a ton of things you can do using Python in FME.

I often use the PythonCaller to make some attributemanipulations inside 1 transformer rather than using 10 different transformers (if elif elif else ..)

You can have very basic PythonCallers like this example which will convert all your attributes to uppercase values:

def upperAll(feature):
    for att in feature.getAttributeList():

I also use the PythonCaller to send emails in case of failure or interact with an FTP Server etc .. There are really no limits

Have fun and happy FMEing


  • Oooohhhhh...I like the sound of this...never thought about using a PythonCaller that way! – Chad Cooper Feb 7 '11 at 20:00
  • Extra question on this one... what program do you have to give access to port 25 (firewall) in order for the email to work? I struggled with this for a couple of weeks and then finally gave up. – blord-castillo Apr 2 '12 at 10:46

Good example above: I'm currently writing an article for our knowledgebase called FMEPedia here: Python and FME Basics.

This includes some simple examples such as deleting a file before running a workspace with a start up script, manipulating features with a PythonCaller etc. There are also links to more complex examples.

Ken Bragg Safe Software



Custom Log

import os.path, time, os, datetime, __main__ , sys, pyfme,shutil
from pyfme import *

class expFeature(object):
    def __init__(self):
        self.logger = pyfme.FMELogfile()

    def close(self):
                #folders creation
                #Log creation
                logFile = param_folder + timecreated +".log"
                FILE = open(logFile,"w")
                log.log("Bla bla bla")

And Send Email:

message = MIMEMultipart()
message["From"]    = email_from
message["To"]      = email_to
message['Date']    = formatdate(localtime=True)
message["Subject"] = subject
message.attach( MIMEText(html, 'html') )
attachment = MIMEBase('application', "octet-stream")
attachment.set_payload( open(FileLog,"rb").read() )
attachment.add_header('Content-Disposition', 'attachment; filename="%s"' %       os.path.basename(FileLog))

smtp = smtplib.SMTP(smtpServer) 
smtp.sendmail(email_from, email_to, message.as_string())         
print "Successfully sent email"

I've recently been using a PythonCaller transformer which gets coordinates from a CSV file and saves them as attributes. The CSV gets written from a another workspace which uses a BoundsExtractor Transformer which gets the bounding coordinates from a bounding box of my area of interest.

I then pass these attributes to other WorkspaceRunners which use the bounding coordinates as a search window for further processing. I have State-Wide data and to process on the whole state would take several hours. Because I limit my processing to a particular window, the whole thing takes one minute.

The pythonCaller code is here:

import fmeobjects
import csv
import re

# Template Function interface:
def getBounds(feature):

    outputDirectory = FME_MacroValues['Output_Directory']   # Set outputDirectory
    NativeTitle = FME_MacroValues['Native_Title'] # Set NativeTitle
    NativeTitle = re.sub('\W','_',NativeTitle)
    NativeTitle = re.sub(' ','_',NativeTitle)

    csvPath = outputDirectory + '\\' + NativeTitle + '_boundingbox.csv'       # Set csvPath

    # open csv file containing bounding coordinates
    with open(csvPath, 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter = ',')
        bounds = reader.next()

    # Set bounding variables
    XMIN = float(bounds[0])
    XMAX = float(bounds[1])
    YMIN = float(bounds[2])
    YMAX = float(bounds[3])    

    # Set attributes to variable values
    feature.setAttribute("_xmin", XMIN)
    feature.setAttribute("_ymin", YMIN)
    feature.setAttribute("_xmax", XMAX)
    feature.setAttribute("_ymax", YMAX)


I also use a python start up script which copies a folder tree to another location if it doesn't exist already.

import os
import fmeobjects
import shutil

srcDir_project = r'W:\AlignmentSheets\PostInstall\Alignment Sheet Generator\ProjectData\ProjectNameFolder'
srcDir_settings = r'W:\AlignmentSheets\PostInstall\Alignment Sheet Generator\ProjectData\Settings'

destBaseDir = FME_MacroValues['Output_Directory']
destDir_project = destBaseDir + '\\' + FME_MacroValues['A_Sheet_Project_Name'] + '\\'
destDir_settings = destBaseDir + '\\Settings\\'

if not os.path.exists(destDir_project):
    print 'Successfully created "%s"' % destDir_project
    print '"%s" Already Exists.  Not Creating Folder.' % destDir_project

if not os.path.exists(destDir_settings):
    print 'Successfully created "%s"' % destDir_settings
    print '"%s" Already Exists.  Not Creating Folder.' % destDir_settings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.