Im trying to batch files / loop?

2187
4
Jump to solution
09-24-2015 10:07 AM
Jackie_Oneil
New Contributor

I have a script that I use to convert text files into LAS files- there are 400 files. How would I make a batch script or loop to tell Python to keep grabbing the next file and keep converting until it reaches the end of the folder? 

0 Kudos
1 Solution

Accepted Solutions
KenTong
New Contributor II

You can loop through a folder to get all your text files.

import os
folder= r'path_to_your_folder_of_text_files'
for root, dirs, files in os.walk(folder):
     for filename in files:
          input_file = os.path.join(root, filename)
          # You can then run your conversion script here
          # Use input_file for your input into your script.


View solution in original post

4 Replies
jasonramsey1
New Contributor II

I would look into using arcpy.da.walk.

DarrenWiens2
MVP Honored Contributor

Or, since this isn't ESRI data specific, you can use regular old os.walk.

0 Kudos
JenniferMcCall4
Occasional Contributor III

Hi Jackie,

You could try something like the following:

import os
for filename in os.listdir('dirname'):
     if os.path.isfile(filename):
          # code to convert file here

0 Kudos
KenTong
New Contributor II

You can loop through a folder to get all your text files.

import os
folder= r'path_to_your_folder_of_text_files'
for root, dirs, files in os.walk(folder):
     for filename in files:
          input_file = os.path.join(root, filename)
          # You can then run your conversion script here
          # Use input_file for your input into your script.