8

I have about 1500 .txt files within a folder named 3410001ne => 3809962sw. I need to subset about 470 of these files to process with a Python script. Below is the section of code prior to my for loop which lists all of the files in a folder:

# Get the list of the text files to process
txt_list = arcpy.ListFiles("*.txt")

How can I subset the folder contents to include 470 of 1500 files ranging from 3609902sw => 3610032sw?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Aaron
  • 51,658
  • 28
  • 154
  • 317

4 Answers4

8

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...
Ed.
  • 141
  • 2
7

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
Roy
  • 3,958
  • 4
  • 30
  • 54
6

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]
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Matthew Snape
  • 6,127
  • 2
  • 27
  • 48
3

you can do it with this way:

import os
import random

yourMainFolder = r'C:/out'
range1 = 3609902sw
range2 = 3610032sw

newAry = []

for a in (int(range1[:-2]) - 1, int(range2[:-2] + 1)):
    newAry.append(str(a) + range2[-2:])

for dirname, dirnames, filenames in os.walk('yourMainFolder'):
    for subdirname in dirnames:
        path = os.path.join(dirname, subdirname)
        if str(subdirname) in newAry:
            files = [f for f in os.listdir(path) if f.endswith('.txt')]

#print files[random.randint(0, 469)]

for a in range(len(files)):
    if a < 470:
         print files[a]

i hope it helps you...

urcm
  • 22,533
  • 4
  • 57
  • 109