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 999335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:20:14+00:00 2026-05-16T07:20:14+00:00

hey guys, beginner here. I have written a program that outputs files to .txt’s

  • 0

hey guys, beginner here. I have written a program that outputs files to .txt’s and am using another to read them and use them. i have used a list to store these values (len(..) gives me 100 for all files). However, whenever i run this:

for w in range(1,20): # i want files file01-file20 excluding file00
    for x in range(100):
        c=c+1 #counter to keep list position on f=0
        exec "f=open('file%02d.txt','r').readlines()"%w #stores data from file00,file01,file02...
        f00=open('file00.txt','r').readlines() #same as ^ but from file00
        for y in range(100):
            xvp=float(f[c].rstrip('\n')) #the error is on this line; the file are stored in vertical order
            pvp=float(f00[y].rstrip('\n')) #maybe even this one
            #and i do stuff with those values...

I get in line 12,
xvp=float(f[c].rstrip(‘\n’))
IndexError: list index out of range

note: there are 100 numbers stored on separate lines in the .txt’s

please, if there is any way to help you help me, let me know
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-05-16T07:20:14+00:00Added an answer on May 16, 2026 at 7:20 am

    You seem to be incrementing c two thousand times (20 times 100 — actually only 1900 times, since range(1,20) will not reach the value 20, as you seem to desire in a comment) — so of course you’re going out of range if you use it to index a list of 100! The whole code is rather a mess and I suggest refactoring it radically, to avoid exec and do things the Python way. Assuming Python 2.6 or better (in 2.5, you need a from __future__ import with_statement at the start of your module):

    f00 = open('file00.txt').readlines()
    for w in range(1, 21):
        for x in range(100):
            with open('file%02d.txt' % w) as f:
                for line in f:
                    xvp = float(line)
                    for line00 in f00:
                        rvp = float(line00)
                        do_stuff(xvp, rvp)
    

    I don’t know if this is the logic you want — coupling every line of file00.txt with each line from the 20 other files — but at least this makes it clear which lines are coupled up with which;-). If what you want is to only couple the first line of file00.txt with the first line from each of the others, then second line with second lines, etc, then add import itertools at the start of your module and change the contents of the with into:

                for line00, line in itertools.izip(f00, f):
                    rvp = float(line00)
                    xvp = float(line)
                    do_stuff(xvp, rvp)
    

    and so forth.

    Note that I’m reading all of file00.txt in memory once and for all (into the f00 list of lines) because apparently you need to loop on those contents more than once, but that’s not needed for the other files.

    An obvious optimization is to convert file00.txt‘s lines to floats only once, replacing the f00 = statement with

    with open('file00.txt') as f:
      rvps = [float(line) for line in f]
    

    then use rvps directly instead of repeating the conversion every time on the strings in f00 — for example, in the second version (the one using itertools.izip):

                for rvp, line in itertools.izip(rvps, f):
                    xvp = float(line)
                    do_stuff(xvp, rvp)
    

    Edit: I see I’ve done a number of tiny enhancements while hardly realizing I was doing so, maybe I’d better explain them;-). No need to pass 'r' when opening a file for reading (can’t hurt, but it’s quite idiomatic to omit it). No need to strip trailing (or for that matter leading) whitespace from a string before calling float on it — float happily skips all such leading and trailing whitespace itself. I did fix what apparently was another bug (you’d never deal with file20.txt) by fixing the applicable range to range(1, 21).

    The with open(...) as f: statements do the opening, bind name f to the open file object, and, as soon as the block of statements they control is finished, guarantee that the file is properly closed — it should almost invariably be used in preference to a stand-alone open, because ensuring all files are closed ASAP is really very good practice (the with statement has many other excellent use cases, but this is the single most frequent one, and the only one that happens to be necessary for this functionality).

    Looping directly on an open file object f (provided the file is opened in text mode, as is the default and applies throughout here), for line in f:, provides one after the other the lines of f (without ever needing to keep them all in memory at once) and is an extremely popular and good Pythonic idiom.

    The construct rvps = [float(line) for line in f], which I use in my recommended optimization, is known as a “list comprehension” and it’s a nicely speedy and compact alternative to a loop that builds a new list.

    itertools.izip, given a number of iterables, provides a single iterable whose items are tuples made by the items of the other iterables “walked in lockstep”. The built-in zip is similar, but (in Python 2) it builds a list in memory, which itertools.izip avoids, so it’s good practice to learn to use the itertools version to avoid wasting memory (not really important for small files like the ones you have, but good habits are best learned and “just applied” rather than having to reflect on them every single time — just one one doesn’t start every morning pondering whether one should brush one’s teeth, but just goes and does so as a matter of good habit;-).

    I’m sure there’s more, but this is what comes to mind off-hand – feel free to ask if I can be of further assistance!

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

Sidebar

Related Questions

Hey guys, I have a little button functionality that is not erring, but also
Hey right now I'm using jQuery and I have some global variables to hold
Hey guys, I have a problem. I have situation where domain in m_domainTable starts
Hey guys, I have a client who wants their front site to have a
Hey guys, first time using stackoverflow. can you guys help me debug? Heres the
Hey guys, I'm using GWT for a data-driven web application, and I'm having issues
hey guys, i'm getting an exception on the following inner exception: {Value cannot be
Hey guys i wrote a quick test. I want delete to call deleteMe which
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey, I'm using Levenshteins algorithm to get distance between source and target string. also

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.