def fileToList(fileName): #by D@CC #on 2024CMar20 #Purpose: read text file into a list & return it f=open(fileName,"r") listRecs=list(f) f.close() return listRecs #def end fileToList def unpackToList(pkgName,moduleName): #by D@CC #on 2024CMar20 #Purpose: read text file into a list & return it #Complete: tested by D@CC #Note: pkgName must be pathed or current folder marker="#::::::::::textpak=>" #print(" 12345678901234567890123") #print("@20 marker:"+marker) #print("@21 moduleName:"+moduleName) f=open(pkgName,"r") newList=[] mLen=len(marker) fullMarker=marker+moduleName lenFullM=len(fullMarker) #print("@27 fullMarker:"+fullMarker) isinModule=False lineNo=0 for line in f: lineNo+=1 if line[0:mLen]==marker: #print("@35",lineNo," line:"+line) if isinModule: isinModule=False #print("@38 ",lineNo," inModule False") #if end if len(line)>=lenFullM : if line[0:lenFullM]==fullMarker : #print("@46, line:"+line) if not isinModule: #print("@48 ",lineNo," inModule True") isinModule=True else: print("@51 didn't turn inModule True") #if end #if end #if end #if end if isinModule: if line[0:lenFullM]==fullMarker : #do not append the marker pass else: #print("@62 ",lineNo," appending") newList.append(line) #if end else: #print("@66 ",lineNo) pass #if end #for end return newList #def end def test_unpackToList(): progName="fileReading.py" #By D@CC #Date: 2024CMar20 #Purpose: To read text files into a list #Re: FRUM function #Repository: sandBox #SSD: _KIOXIA_GTLL #FRUM will use this to retrieve a module into a returned list #Parm0: pathed pkg name #parm1: module name to fetch fName=input("fName?:") f=open(fName,"r") print(" 1 2") print(" 12345678901234567890") #Conclusion 01: there is a LF at end of last line # of a text file # 02: the LF is \n # 03: no CR in Linux text files # 04: for thing in f: # 05: file contains only first line # 06: thing contains the whole file # 07: each line is followed by a LF # 08: there is no LF before line 1 # 09: the LF after the last line # "looks like" an empty line # 10: thing is not list # 11: thing2=list(f) gives: # thing2: ['#::::::::::textpak>file.txt\n', 'line 1\n', 'line 2\n', 'last line\n'] # 12: typeThing2: # 13: newList2: of .../ix/pPkg_py.py # newList2: ['#::::::::::textpak=>[pPkg_py.py]\n', 'class by: D@CC\n', etc ] for line in f: #line=line.strip() #strips all including the LFs #line=line.strip("\n") #strips only the LFs print("@118 line:"+line+":") #for end print("eof:"+fName) f.close() print("@122 closed and open") f=open(fName,"r") for file in f: #file contains first line only print("@126 file:",file) thing=f.read() print("@128 thing:",thing) print("eof:"+fName) typeThing=type(thing) print("@131 typeThing:",typeThing) f.close() print("@133 closed and open") f=open(fName,"r") thing2=list(f) print(" 1 2") print(" 12345678901234567890") print("@138 thing2:",thing2) typeThing2=type(thing2) print("@140 typeThing2:",typeThing2) f.close() print("@142 closed") newList=fileToList(fName) print("@144 newList:",newList) typeNewList=type(newList) print("@146 typeNewList:",typeNewList) pkgName="/home/pi/Desktop/IX_assets/ix/pPkg_py.py" moduleName=input("moduleName? :") #moduleName="[pPkg_py.py]" newList2=unpackToList(pkgName,moduleName) lenNewList2=len(newList2) print("@152 lenNewList2:",lenNewList2) print("@153 newList2:",newList2) print("eop:"+progName) return #def end test_unpackToList if __name__ == "__main__" : test_unpackToList() #/unpackToList.py