Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9011145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:39:07+00:00 2026-06-16T02:39:07+00:00

I am writing file to disk but before i write i collect all the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:39:09+00:00Added an answer on June 16, 2026 at 2:39 am

    You are writing over your text variable in each loop of the for loop. You should append each line to the text variable:

    def makeBatFile(self):
        text=""
        for each in xrange(self.listWidget.count()):
            text += "echo [Task Id:%s]\n" % each
            text += self.listWidget.item(each).text() +"\n"
            print text
        self.writeBatFile("batch",text)
    

    the += operator is a shortcut for:

    text = text + othertext
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a file to disk in stages. As I write it I
My Perl program takes some text from a disk file as input, wraps it
I'm reading a file from a SQL server and writing it to disk temporarily
I find myself writing file and directory utility functions all the time, and I
How can I write data to stdout instead of writing file? I'm using GPG
How would you go about writing a file from emacs that contains only the
I have a client node writing a file to a hard disk that is
I want to set a variable from a flash file so that I can
I am trying to write an XML file but it is too large to
I'm writing file save and load functions for a specific file format that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.