System.file.writeFile with append is writing on the same line?

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?

Does anything change if you use os.linesep in place of the \n? Might make a difference if you’re on Windows.

EDIT: actually, the docs right there indicate that the proper thing to do here is to use \n as you’re doing, regardless of platform…

1 Like

I am on Windows Server 2012, adding os.linesep to the end of the string fixed my issue. Thanks for the quick reply!

EDIT: actually, the docs right there indicate that the proper thing to do here is to use \n as you’re doing, regardless of platform…

Definitely was not the case, at least with this version. os.linesep was was a \r\n fwiw

Glad that worked! As you can see above, I second guessed myself based on the guidance in the python docs… Looks like intuition won this time… :slight_smile:

1 Like

Who needs documentation anyway :sunglasses:

Kidding, but much appreciated for the answer.