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

  • Home
  • SEARCH
  • 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 8289597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:32:35+00:00 2026-06-08T12:32:35+00:00

This is a continuation from a question that I had yesterday. I am trying

  • 0

This is a continuation from a question that I had yesterday. I am trying to open a text file that contains a list of numbers. I want to write these numbers to a new file multiple times. The purpose of the loop is to be able to write the source list multiple times. In this example, I want to print a list of 100 numbers 10 times so I end up with a list of 1000 numbers in my output file. This is the code that I am working with:

i = 10
while i > 0:
    with open ('C:/TestReq_100 Records.txt', 'r') as ipf:
       for line in ipf:
            num = line.strip()
            filename = 'processed.txt'
            with open('processed.txt', 'w') as opf:
                opf('%s' %num)


##           print num
    i = i - 1

If I comment out the code related to writing to file and use the print command in the interpreter, the code does what I want. I just can’t seem to get the same output in a text file. Again, I am not a student. Just trying to create files for use in my Company’s software for testing purposes…thanks!

  • 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-08T12:32:37+00:00Added an answer on June 8, 2026 at 12:32 pm

    In addition to Levon’s answer, there are a few issues with your current program.

    • Rather than manually initializing and decrementing an index, try using xrange to accomplish the same task in a more concise way.

    • You’re overwriting processed.txt for each number; you should open it at the same time as the input file.

    • What is the purpose of i? As it stands, your program is going to do the exact same thing 10 times because i is never used inside the loop.

    • '%s' % num does nothing special when num is already a string.

    That said, here’s a cleaner version of your program, although i still does nothing in particular:

    for i in xrange(10, 0, -1):
        with open('C:/TestReq_100 Records.txt', 'r') as ipf:
            with open('processed.txt', 'w') as opf:
                for line in ipf:
                    num = line.strip()
                    opf.write(num)
    

    You may want to clarify the intended output of this program for a better answer.

    EDIT: Here is a more efficient solution for copying a file into a new file multiple times:

    # Read the input data once.
    with open('C:/TestReq_100 Records.txt', 'r') as ipf:
        ipdata = ipf.read()
    # Add a newline to the end of the file if there isn't already one present.
    # Without this, the last and first lines of the input might become single lines.
    if ipdata[-1] != '\n':
        ipdata += '\n'
    # Write to the output file multiple times.
    with open('processed.txt', 'w') as opf:
        for i in xrange(10):
            opf.write(ipdata)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a continuation question I had from a post yesterday: Initializing NSMutableDictionary (It's
This is a continuation of this question from yesterday . Here are my three
A continuation from this question I need a SQL statement that returns the number
In a continuation from this question . I'm trying to bind a given function
This is basically a continuation from my previous question . Seeing that my fixed
Ok - this is in continuation from my earlier question about sending an email
This is a continuation from a previous stackoverflow question. I've renamed some variables so
Ok this is a continuation from this question: How to make a simple Hello
As a continuation in my thought patterns from this question: Saxon in Java: XSLT
This is a continuation from an existing question. Javascript - Goto URL based on

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.