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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:04:44+00:00 2026-05-19T11:04:44+00:00

I have a list of approximately 200 000+ objects, each one representing a file

  • 0

I have a list of approximately 200 000+ objects, each one representing a file (but not actually holding the file’s contents, just the full path name and date).

The program I am writing copies any subset of these files, depending on the user-provided date range. I first create a list of all of the files in the source directory (with the glob module), create an instance of my file-representation class and add that instance to a list, like so:

for f in glob.glob(srcdir + "/*.txt"):
    LOG_FILES.append(LogFile(f))

Now, to keep the copying of files quick and the block of code clean, I remove the LogFile objects that do not fit inside of the date range.

for i in xrange(0, len(LOG_FILES)):
    if LOG_FILES[i].DATE < from_date or LOG_FILES[i].DATE > to_date:
        del(LOG_FILES[i])

Afterwards, I can just copy the files that are left in the list:

for logfile in LOG_FILES:
    os.copy(logfile.PATH, destdir)

The issue occurs with the for i in xrange... example: I get thrown an IndexError when the value of i gets to 63792.

IndexError: list index out of range.

Any ideas?

EDIT Thank you very much for the quick responses! Now that I think about it, it was a silly oversight on my part. Again, thank you, everyone. 🙂

  • 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-19T11:04:45+00:00Added an answer on May 19, 2026 at 11:04 am

    [EDIT] Oops, I forgot to invert the “<” and “>” and add an ‘equals’ sign.

    LOG_FILES = [LogFile(f) for f in glob.glob(srcdir + "/*.txt")
                            if from_date <= f.DATE <= to_date]
    

    This can replace the whole initalization of LOG_FILES. It’s a list comprehension (if you wish you can make it a generator (which doesn’t get evaluated until it’s enumerated) by replacing the [ ] with ( ). That might be more efficient depending on what you do with it.

    You need to do this because editing a collection while enumerating it isn’t allowed. (see above, far more eloquent answers).

    You can read the expression above like this:

    “create a list (or enumerable) of the result of LogFile, when it’s handed ‘f’ for each f in ‘glob.glob(…)’ but only if the ‘if’ statement is true.”

    See: The List Comprehension section of that link.

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

Sidebar

Related Questions

I have a really big property list file(approximately 2 MB large) and I need
I have approximately 30 variadic functions. Each one accepts a path as the final
I have list of files which contain particular patterns, but those files have been
I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
I need one that does not have a big queue. So, if it does
I have a list of approximately 300 words and a huge amount of text
I have a list of textual descriptions, each of which a user must score
I have approximately 5,000 matrices with the same number of rows and varying numbers
I have a list of records in my database and each record is associated
I have a list of approximately 4300 URL's, all very similiar. It is likely

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.