在事件指令碼中使用檔案系統物件

您可以使用 Jython 檔案系統物件來處理檔案和資料夾。以下範例使用檔案系統物件建立檔案以及將現有檔案的內容複製到新檔案。

Read the following Input File
Entity,Currency,ICP,Product,Store,Channel,Custom4,Custom5,Custom6,Custom7,UnitsSold,Sales
EastSales, USD, [ICP None], H740, Comma_Phone_Stores, Retail_Direct, [None],[None],[None],[None],127,9954.103768
EastSales, USD, [ICP None], H740, Freds, National_Accts, [None],[None],[None],[None],112,6610.371552
EastSales, USD, [ICP None], H740, Good_Buy, National_Accts, [None],[None],[None],[None],112,6610.371552
Write the following Output File
EastSales, USD, [ICP None], H740, Comma_Phone_Stores, Retail_Direct, [None],[None],[None],[None],UnitsSold,127
EastSales, USD, [ICP None], H740, Comma_Phone_Stores, Retail_Direct, [None],[None],[None],[None],Sales,9954.103768
EastSales, USD, [ICP None], H740, Freds, National_Accts, [None],[None],[None],[None],UnitsSold112
EastSales, USD, [ICP None], H740, Freds, National_Accts, [None],[None],[None],[None],Sales6610.371552
EastSales, USD, [ICP None], H740, Good_Buy, National_Accts, [None],[None],[None],[None],UnitsSold,112
EastSales, USD, [ICP None], H740, Good_Buy, National_Accts, [None],[None],[None],[None],Sales,6610.371552
infilename = fdmContext["INBOXDIR"]+"/InputFile.txt"
outfilename = fdmContext["INBOXDIR"]+"/DataFile.txt"
infile = open(infilename, "r")
outfile = open(outfilename, "w")
for line in infile:
  column = line.rsplit(',',2)
  if column[2].strip() != "Sales" :
    outfile.write(column[0] + ",UnitsSold," + column[1] + "\n")
    outfile.write(column[0] + ",Sales," + column[2])
outfile.close()