Search code examples
pythonpython-2.7arcpy

How to use raw_input() with while-loop?


my task is to execute multiple buffers with the list-loop!

import arcpy

arcpy.env.overwriteOutput = 1  
arcpy.env.workspace="C:\\salzburg.gdb"  
sbgRivers="sbg_rivers"
buff_name=raw_input("Please insert a file name:")  
for buffer_size in [100,200,300,450]:  
    outfile="C:\\salzburg.gdb\\buffer_output"
    arcpy.Buffer_analysis(sbgRivers,buff_name+str(buffer_size),buffer_size)  
    print "Buffer complete"

So the next step is: If the file name already exists(raw_input), the user is asked to enter a new data set name until a name is found, which doesn´t exist yet!

I thought about a while loop, but i´m not sure how to integrate it in the code above

i ended up with this

import arcpy
arcpy.env.overwriteOutput = 1  
arcpy.env.workspace="C:\\salzburg.gdb"  
sbgRivers="sbg_rivers"
buff_name=raw_input("Please insert a new data set name:")
while arcpy.Exists(buff_name):    
    buff_name=raw_input("Please enter a new data set name")  
    for buffer_size in [100,200,300,450]:    
         arcpy.Buffer_analysis(sbgRivers,buff_name+str(buffer_size),buffer_size) 
         print "Buffer complete"

What do you think? Any help is welcome and appreciated!


Solution

  • You would want to first make sure that you have the correct filename and then proceed to the next step. The code should be: (assuming arcpy.Exists(buff_name) does what it is supposed to do, because I think it should be arcpy.Exists(buff_name+str(100)) or something like it.)

    import arcpy
    arcpy.env.overwriteOutput = 1  
    arcpy.env.workspace="C:\\salzburg.gdb"  
    sbgRivers="sbg_rivers"
    buff_name=raw_input("Please insert a new data set name:")
    while arcpy.Exists(buff_name):    
        buff_name=raw_input("Name already exists. Please enter a new data set name")  
    for buffer_size in [100,200,300,450]:    
        arcpy.Buffer_analysis(sbgRivers,buff_name+str(buffer_size),buffer_size) 
        print "Buffer complete"
    

    Your previous code would have run the for loop for all the inputs provided (even the wrong ones).