Python 脚本 运行 FME workbench

Python script to run FME workbench

我有超过 500 个 xml 文件,每个 xml 文件应该在 FME workbench 上单独处理(每个 xml 的 FME workbench 迭代文件)。 对于这样的提议,我必须运行一个python文件(loop.py)为每个xml文件迭代FMEworkbench。

整个过程过去在其他 PC 上运行没有任何问题。现在,一旦我 运行 模块出现以下错误:

Traceback(最后一次调用):E:\XML_Data 文件“E:\XML_Data\process_XML_Tile_1.py”,第 28 行,位于 如果在 open(path_log + "\" + data + ".log").read() 中显示“翻译成功”: IOError:[Errno 2] 没有这样的文件或目录:'E:\XML_Data\data_out\log_01\re_3385-5275.xml.log'

附上python代码(loop.py)。

非常感谢任何帮助。

import os
import time
# Mainpath and Working Folder:
#path_main = r"E:\XML_Data"
path_main = r"E:\XML_Data"
teil = str("01")

# variables
path_in = path_main + r"\data_in_Places\teil_" + teil                # "Source folder of  XML files"
path_in_tile10 = path_main + r"\data_in_Tilingx10.shp"            # "Source folder of Grid shapefile"
path_in_commu = path_main + r"\data_in_Communities\Communities.shp"  # "Source folder of Communities shapefile"
path_out = path_main + r"\data_out\teil_" + teil                        # "Output folder of shapefiles that resulted from XML files (tile_01 folder)"
path_log = path_main + r"\data_out\log_" + teil                         # "Output folder of log files for each run(log_01 folder)"
path_fme = r"%FME_EXE_2015%"                                            # "C:\Program Files\FME2015\fme.exe"
path_fme_workbench = path_main + r"\process\PY_FME2015.fmw"             # "path of FME workbench"
datalists = os.listdir(path_in)
count = 0

# loop each file individually in FME
for data in datalists:
    if data.find(".xml") != -1:
        count +=1
        print ("Run-No." + str(count) + ": with data " + data)
        os.system (path_fme + " " + path_fme_workbench + " " + "--SourceDataset_XML"+ " " + path_in + "\" + data + " " + "--SourceDataset_SHAPE" + " " + path_in_tile10 + " " + "--SourceDataset_SHAPE_COMU" + " " + path_in_commu + " " + "--DestDataset_SHAPE" +" " +path_out + " " +"LOG_FILENAME" + " " + path_log + "\" + data + ".log" )
        print ("Data processed: " + data)
        shape = str(data[19:28]) + "_POPINT_CENTR_UTM32N.shp"
        print ("ResultsFileName: " + shape)
        if "Translation was SUCCESSFUL" in open(path_log + "\" + data + ".log").read():
            # Translation was successful and SHP file exists:
            if os.path.isfile(path_out + "\" + shape):
                write_log = open(path_out + "\" + "result_xml.log", "a")
                write_log.write(time.asctime(time.localtime()) + " " + shape + "\n")
                write_log.close()
                print("Everything ok")
            #Translation was successful, but SHP file does not exist:
            else:
                write_log = open(path_out + "\" + "error_xml.log", "a")
                write_log.write(time.asctime(time.localtime()) + " Data: " + shape + " unavailable.\n")
                write_log.close()      
        # Translation was not successful:
        else:
            write_log = open(path_out + "\" + "error_xml.log", "a")
            write_log.write(time.asctime(time.localtime()) + " Translation " + Data + " not successful.\n")
            write_log.close()
        
print ("Number of calculated files: " + str(count))

脚本很可能在 os.system 行失败,因此日志文件不是从命令创建的。既然你说的是不同的电脑,可能是多种原因造成的,比如FME的版本不同(所以环境变量%FME_EXE_2015%就不会存在)。

使用工作区运行器转换器来执行此操作。

FME版本是outdated.so首先检查版本是否会产生问题。