def toSerialFileFromList(fileName,listName): #By: d@CC #On: 2024CMar23 #parm0 fileName: serial file to create #parm1 listName: list to write to serial file #NB: This very important python function # is accompanied by many checks & conditions # in an effort to prevent bad files from # being created, but throwing a minimum # number of errors. # #Purpose: To create a pathed serial file # from a Python List. The python List # can have line feeds. If so, double # line feeds will NOT be written to the # serial file. A full path is exprected to # be included in the file name, if not # the file will be written to the current # folder. An empty list will create an # empty file with a name but no length. # An invalid fileName will throw an error. # Very long text lines will NOT throw # an error. Zipped files, object (compiled) # files etc should not be created. An # end of file marker should not occur # in the list. An empty record (containing # only a line feed) is a valid record and # results from a list object that is null. # Multiple line feeds will not be written # to the serial file. Superfluous line # feeds will be removed. Multiple # python definitions are permitted in a # list but this is not recommended. # print("BEWARE: w/o any error checking") f=open(fileName,"w") lineCnt=0 for a in listName: lineCnt+=1 f.write(a) #for end f.close() print("list was written to :"+fileName) print("end of program") f.close() return #def end def toSerialFileFromList_test(): #by D@CC #on 2024CMar23 #status: tested w/o any error checking print("begin: toSerialFileFromList_test") listName=[ 'record 1\n', 'last record\n' ] fileName="testList.txt" toSerialFileFromList(fileName,listName) fR=open(fileName,"r") print(":", end=' ') for line in fR: print("line:"+line+":", end=' ') fR.close() print("end of "+fileName) print("end of toSerialFileFromList_test") return #def end toSerialFileFromList_test if __name__ == "__main__" : toSerialFileFromList_test() #/toSerialFileFromList.py.txt