Subset Folder Contents ArcPy

You could skip using ap.Listfiles all together and use a for loop with xrange...

for rt in xrange(3609902,3610032):
quads = ["%snw.txt"%rt,"%sne.txt"%rt,"%ssw.txt"%rt,"%sse.txt"%rt]
print quads
for quad in quads:
    if ap.Exists(quad):
        # Do whatever...

For starters, you could include sw in your wildcard statement (*sw.txt), which presumably would reduce your number of returned records substantially (assuming you have ne, nw, se, etc).

Second, now that you're working with a subset of files, use a conditional statement to widdle down your files to your exact needs.

Pseudo Code:

  1. Set workspace (directory)
  2. For loop with your txt_list var
  3. Create a variable that stores the first 7 characters in the file name (ie. theNumbers = theFile[:6])
  4. Cast the variable as an integer: int(theNumbers)
  5. Test (conditional) that your variable is greater than or equal to 3609902 and less than or equal to 3610032
  6. If YES, do whatever needs to be done

Based on explanation by Roy:

toprocess = [p for p in [int(filename[0:7]) for filename in txt_list] if p>=3609902 and p<=3610032]

Tags:

Arcpy