Cannot open (txt) file in second loop

481
4
08-24-2017 04:36 AM
Nicole_Ueberschär
Esri Regular Contributor

It's me again with lots of cruises/campaigns, points and txt files but now I have additionally polygons to create. 

This is my main script: 

def main():
    # Step 1: find the source files to process
    list_of_files=[]
    list_of_files = FindFiles(location_for_tables)
    
    # Step2: prozess each file to data for points
    for each_file in list_of_files:
        # create and save sub tables
        store_table(each_file)

    # Step3: prozess each file to create a point in kivupoints
    list_of_tables = FindFiles(DirOutTables)
    for each_table in list_of_tables:
        # create and store the point
        tview = CreateTableView(DirOutTables, each_table)
        pointInfo = ReadPointInfo(tview)
        uid = LoadPointInfoToClass(pointInfo)
    
    # Step4: select points per campaign and create a polygon
    for each_campaign in list_of_files:
        arcpy.AddMessage(list_of_files)
        CreatePolygonFromCampaign(each_campaign)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

In Step2 and Step4 I am using the same files. But because I have to use the result from Step3 in Step4 I can't use the same for loop for Step2 and Step4. In both functions store_table(each_file) and CreatePolygonFromCampaign(each_campaign) I ask for the uniqueValues of one attribute of the current file. While this works perfectly with profile_nrs=unique_values(table, "Station") in the store_table(each_file) function (table receives the value from each_file), in the CreatePolygonFromCampaign(each_campaign) it tells me the correct file name but stops the script with the error message "cannot open 'KivuWatt_151120_Near-plant_CTM257 (2).txt'" (which is the correct txt file) while trying to retrieve the unique values for the Cruise attribute. I also tried other attributes and other files but with the same result. 

I wonder if the file is somehow "blocked" by the first loop or the first uniqueValues() method since it's using a SearchCursor. But it's using a "with arcpy.da.SearchCursor(table, [field]) as cursor" so I would assume it should release/delete the cursors afterwards again...

Any help on this will be highly appreciated. 

I think I provided all necessary information and script snippets but please let me know if you need more details. 

Thanks in advance!

Tags (2)
0 Kudos
4 Replies
Nicole_Ueberschär
Esri Regular Contributor

Update: I changed Step4 using new variable names for the list and its items. Now it works. Can someone maybe explain to me why I can't use the same loop/variables?

# Step4: select points per campaign and create a polygon
    list_of_campaigns = FindFiles(location_for_tables)
    for each_campaign in list_of_campaigns:
        arcpy.AddMessage(list_of_campaigns)
        CreatePolygonFromCampaign(each_campaign)
0 Kudos
DanPatterson_Retired
MVP Emeritus

Find_files function may hold the key, is it a generator expression or something? It sounds like the values needed to be reset

0 Kudos
Nicole_Ueberschär
Esri Regular Contributor

Thanks for your time Dan. 

It is a function: 

def FindFiles(location):
    arcpy.env.workspace = location
    listingFiles=arcpy.ListFiles("*.txt")
    return listingFiles

maybe the return value makes it get blocked somehow?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Without knowing what your other custom functions are doing, exactly, it is hard to say why.

0 Kudos