Hi I am looking for information on how to appropriately use the FME Command in a python loop using subprocess.popen to call the argument. I am struggling to find info on how to achieve this. I am getting syntax warnings in my command when I use subproces.popen. But if I past directly to the cmd prompt I dont have issues. So Im missing something. Any help is appreciated. Please ignore all my comments :P
Page 1 / 1
Oh no I posted without the screenshot
import subprocess
fme_ = r"E:\Program Files\FME\fme.exe" ##fme.exe
_Workbench = r"your path to workbench\FME_option_poc.fmw"
Â
arg1 = r"path to .gdb"
arg2Â =Â ""
arg3 = r"your path to csv.csv"
arg4Â =Â "Complete"
Â
command = f'"{fme_}" "{_Workbench}" --SourceDataset_GEODATABASE_FILE "{arg1}" --False_Time "{arg2}" --DestDataset_CSV2 "{arg3}" --FME_Status_Entry "{arg4}"'
print (command)
starter = subprocess.STARTUPINFO()
starter.dwFlags |= subprocess.STARTF_USESHOWWINDOW
Â
process = subprocess.Popen(command,startupinfo=starter, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate() # executes command and gives you the standard out and standard error if you want.
Â
Here is a python function example that also retrieves the standard out and standard error messages from the workbench - advanced.
def sub__fmeworker(arg1, arg2):
    """ command variable object houses the subprocess cmd string line.
    1. _workbench = fme workbench.fmw, 2. arg1, 3. arg2
Â
    The subprocess.Popen() executes the command and retrieves the process outputs to stdout and stderr."""
    text = ''
Â
    try:
        fme_ = r"C:\Program Files\FME\fme.exe" ##fme.exe
        command = f'"{fme_}" "{_Workbench}" --FEATURE_TYPES "{arg1}" --LogFile "{arg2}"'
        starter = subprocess.STARTUPINFO()
        starter.dwFlags |= subprocess.STARTF_USESHOWWINDOW
Â
        process = subprocess.Popen(command,startupinfo=starter, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()
        if stderr:
            stderr_de = str(stderr.decode("utf-8")) # Need to remove byte object
            msg = stderr_de.split("\r\n")Â0]
            text += arg1 +'  '+ msg
        else:
            stdout_de = str(stdout.decode("utf-8")) # Need to remove byte object
            msg = stdout_de.split("\r\n")
            text += msg+'ERROR########'
    except:  #if the workbench fails add error tag
        text += arg1+'ERROR########'
    return text
Â
import subprocess
fme_ = r"E:\Program Files\FME\fme.exe" ##fme.exe
_Workbench = r"your path to workbench\FME_option_poc.fmw"
Â
arg1 = r"path to .gdb"
arg2Â =Â ""
arg3 = r"your path to csv.csv"
arg4Â =Â "Complete"
Â
command = f'"{fme_}" "{_Workbench}" --SourceDataset_GEODATABASE_FILE "{arg1}" --False_Time "{arg2}" --DestDataset_CSV2 "{arg3}" --FME_Status_Entry "{arg4}"'
print (command)
starter = subprocess.STARTUPINFO()
starter.dwFlags |= subprocess.STARTF_USESHOWWINDOW
Â
process = subprocess.Popen(command,startupinfo=starter, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate() # executes command and gives you the standard out and standard error if you want.
Â
Here is a python function example that also retrieves the standard out and standard error messages from the workbench - advanced.
def sub__fmeworker(arg1, arg2):
    """ command variable object houses the subprocess cmd string line.
    1. _workbench = fme workbench.fmw, 2. arg1, 3. arg2
Â
    The subprocess.Popen() executes the command and retrieves the process outputs to stdout and stderr."""
    text = ''
Â
    try:
        fme_ = r"C:\Program Files\FME\fme.exe" ##fme.exe
        command = f'"{fme_}" "{_Workbench}" --FEATURE_TYPES "{arg1}" --LogFile "{arg2}"'
        starter = subprocess.STARTUPINFO()
        starter.dwFlags |= subprocess.STARTF_USESHOWWINDOW
Â
        process = subprocess.Popen(command,startupinfo=starter, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()
        if stderr:
            stderr_de = str(stderr.decode("utf-8")) # Need to remove byte object
            msg = stderr_de.split("\r\n")Â0]
            text += arg1 +'  '+ msg
        else:
            stdout_de = str(stdout.decode("utf-8")) # Need to remove byte object
            msg = stdout_de.split("\r\n")
            text += msg+'ERROR########'
    except:  #if the workbench fails add error tag
        text += arg1+'ERROR########'
    return text
Â
Thank you!Â
I found the error in my code and it was in the cmd.Â