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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:29:36+00:00 2026-06-14T17:29:36+00:00

I have a for loop that runs through a directory and processes the files

  • 0

I have a for loop that runs through a directory and processes the files there, but I’d like to only process a certain number of the files at one time. For example, I have a directory with 1000 files, but I can only process 250 of them a day, so the first time I run the script, it processes the first 250. then the next 250, and so on and so forth.

First, I’m checking the file names against an XML file that records the name of files that have already been synced, so that I don’t process them a second time. Then I would like to process the next n files, where I have a variable synclimit = n

I thought about adding the in range statement to the for loop like this:

tree = ET.parse("sync_list.xml")
root = tree.getroot()
synced = [elt.text for elt in root.findall('synced/sfile')]
for filename in os.listdir(filepath) and in range (0, synclimit) :
    if fnmatch.fnmatch(filename, '*.txt') and filename not in synced:
    filename = os.path.join(filepath, filename)
    result = plistlib.readPlist(filename)

But, I’m pretty sure this will only check the first n number of files in the directory each time. Should I add the range statement to the if statement? like:

tree = ET.parse("sync_list.xml")
root = tree.getroot()
synced = [elt.text for elt in root.findall('synced/sfile')]
for filename in os.listdir(filepath):
    if fnmatch.fnmatch(filename, '*.txt') and filename not in synced and in range (0, synclimit):
    filename = os.path.join(filepath, filename)
    result = plistlib.readPlist(filename)

or is there an easier way to do this? Thank you.

  • 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-14T17:29:37+00:00Added an answer on June 14, 2026 at 5:29 pm

    Just keep a separate counter and increment that, then test if it has reached synclimit. Simple as that. There is no need to get too clever here:

    processed = 0
    for filename in os.listdir(filepath):
        if not filename.endswith('.txt') or filename in synched:
            continue
        # process
        processed += 1
        if processed >= synclimit:
            break  # done for today.
    

    Alternatively, since os.listdir() returns a list, you could filter it if you have your already synched list of filenames in a set, then slice it down to your maximum size:

    synced = set(elt.text for elt in root.findall('synced/sfile'))
    to_process = [f for f in os.listdir(filepath) if f.endswith('.txt') and f not in synched]
    
    for filename in to_process[:synclimit]:
        # process
    

    Note that I just test for .endswith('.txt') instead of using your simple filematcher; the test comes down to the same thing.

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

Sidebar

Related Questions

I have a loop that runs through a variety of websites and I'd like
I have a loop that runs through a text area's line and processes each
I have a jar that when run, goes through the files in a directory
i have a program(winform) that runs through a lot of files(in the area of
I have a loop that runs through each column and sets the value to
I am building a chess game and i have a loop that runs through
I have a while loop that runs through a table collecting all rows that
I have a for loop that runs through an array of functions and invokes
I have a loop that runs through an array of image views, adding an
I have a jQuery each loop that runs through a list of three JSONP

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.