Working on an older system. Rewriting some text file export logic that was misusing jython file commands that would sometimes error and then the file would be unusable as Ignition had it “open” technically.
I query a dataset from the database and from there, row by row, I write it to a file using system.file.writeFile(filePath, line, 1)
. Here’s the actual code
def createDataRecord(data, filename_date, currentDateObj, destinationFolder):
"""
Creates data record for the day. Mostly copied over from original script but uses system.file to avoid file lockups.
Args:
data: dataset from query
filename_date: str, t_stamp to put onto file
currentdateobj: java.lang.Date, to be saved in file after formatting
destinationFolder: str, where we are saving this file
Returns:
Nothing, or throws error that will be caught by main call
"""
filename = "Data%s.txt" %(filename_date)
fullpath = destinationFolder + filename
timestamp = system.db.dateFormat(currentDateObj, "MM/dd/yyyy hh:mm:ss a")
for rec in data:
materialCode = rec["materialCode"]
materialDesc = rec["materialDesc"]
unitADcode = rec["bottleBarcode"]
caseADcode = rec["caseBarcode"]
palletADcode = rec["palletBarcode"]
palletADcode = palletADcode[-13:]
ad_timestamp = system.db.dateFormat(rec["bottle_tstamp"],"MM/dd/yyyy hh:mm:ss a")
entry = "someSite,%s,%s,%s,,%s,%s,P,%s,%s \n" %(materialCode, materialDesc, unitADcode, caseADcode, palletADcode, ad_timestamp, timestamp)
# The 1 indicates to append to the current file
system.file.writeFile(fullpath, entry, 1)
And here is the file that gets produced -
as you can see the second line starts on the first line, and then everything gets shifted off a bit.
I tried the same code with two \n\n
at the end but I got the same result. I don’t know how to fix this.
Also this system is not currently able to be upgraded so any answers please be within the scope of 7.8.2.
I guess I could go back to writing with the jython file writer with a context manager instead if this is a known bug in 7.8.2, but I do prefer sticking to Ignition builtins when possible. I’ve never seen this behavior from system.file.writeFile before.
Any ideas?