Python Forum
Line by line execution - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Line by line execution (/thread-6875.html)



Line by line execution - Filthy_McNasty - Dec-11-2017

I have a script that does the job but only for one file. Just to explain what`s going on here:

 
import sys
sys.path.append("C:\\Program Files\\FME\\fmeobjects\\python27")

import fmeobjects
runner = fmeobjects.FMEWorkspaceRunner()
workspace = [font="Calibri",sans-serif]'C:\FME\Project_1.fmw'[/font]
parameters = {}
parameters['SourceDataset_ACAD'] =[font="Calibri",sans-serif]'C:\AutoCAD\Project_1.dwg'[/font]
parameters['DestDataset_OGCKML'] =[font="Calibri",sans-serif]'C:\Maps_KMZ\Project_1.kmz'  [/font]

runner.runWithParameters(workspace, parameters)

try:
    # Run Workspace with parameters set in above directory
    runner.runWithParameters(workspace, parameters)
    # or use promptRun to prompt for published parameters
    #runner.promptRun(workspace)
except fmeobjects.FMEException as ex:
    # Print out FME Exception if workspace failed
    print ex.message
else:
    #Tell user the workspace ran
    print('The Workspace is ran successfully'.format(workspace))
runner = None
This script executes FMW file that does conversion from AutoCAD DWG  (C:\AutoCAD) to KMZ file and stores it in C:\Maps_KMZ folder. Now, I need to do the same thing for about 20-ish FME files that are in the same source folder.
Is it possible to  execute each file at the time and add specific time frame between two executions let's say 2 minute pause between them,  because I can not run 2 or more conversions at the same time, it would crash Windows. If so, how to do it. I don`t have much Python experience so if you can help me directly with the code that would be fantastic.

Disregard this [font ...etc] stuff , NOT a part of the code, I cannot figure out how to delete that from the code, this is so confusing pasting code in here.


Thank you very much for your help!


RE: Line by line execution - wavic - Dec-11-2017

import os.path
from glob import glob

# glob(pattern) returns all files according to the pattern
for dwg in glob("/path/*.dwg"): # the path can be set in Unix style regardless of the OS
    in_file = os.path.abspath(dwg) # get the absolute path of a file. For example: 'C:\AutoCAD\Project_1.dwg'

    # os.path.splittext(file_name) separate the the file name from the extension and you get ['my_file', '.txt']
    out_file = "{}{}".format(os.path.splittext(in_file)[0], '.kmz')

    # os.path.join(path, file_name) joins the path with the file name properly formated with '/'
    out_full_path = os.path.join('C/Maps_KMZ', out_file)

    # Parameters
    parameters['SourceDataset_ACAD'] =[font="Calibri",sans-serif]"{}".format(in_file)[/font]
    parameters['DestDataset_OGCKML'] =[font="Calibri",sans-serif]"{}".format(out_full_path)  [/font]

    # and so on
    # ...
Something like this should work