I am writing file to disk but before i write i collect all the items from QListWidget to text variable with each line separated by “\n” but instead of getting all the lines i get only last line:
def makeBatFile(self):
text=""
for each in xrange(self.listWidget.count()):
text="echo [Task Id:%s]\n" % each
text=text+ self.listWidget.item(each).text() +"\n"
print text
self.writeBatFile("batch",text)
although the print inside the for loop prints all the line but i cannot make the call to writeBatfile method from within the for loop because it will try to write the file the number of items in the list when i want all the list items to be written in one file…
def writeBatFile(self,do="single",task=None):
self.task=task
now = datetime.datetime.now()
buildCrntTime=str(now.hour) +"_" + str(now.minute)
selected=str(self.scnFilePath.text())
quikBatNam=os.path.basename(selected).split(".")[0]+"_"+buildCrntTime+".bat"
if do !="batch":
self.batfiletoSave=os.path.join(os.path.split(selected)[0],quikBatNam)
self.task = str(self.makeBatTask())
else:
self.batfiletoSave=os.path.join(self.batsDir,buildCrntTime+".bat")
try:
writeBat=open(self.batfiletoSave,'w')
writeBat.write(self.task)
self.execRender()
except: pass
finally: writeBat.close()
What am I doing wrong while building the content to be passed to writeBatFile method?
You are writing over your text variable in each loop of the for loop. You should append each line to the text variable:
the += operator is a shortcut for:
This way you are appending the string to the text variable in each iteration of the loop instead of reassigning the variable to the new string.