2015-09-04 2 views
0

Я сталкиваясь с двумя проблемами при удалении файлов внутри папки под названием SelectionУдаление файлов в папке, но не папки

  1. Сценарий предполагают, чтобы удалить только файлы внутри Selection папки, но он удаляет папку Selection.

  2. Папка Selection была удалена с самого начала запуска скрипта, bu Я положил процесс в конец. На самом деле мне нужно проанализировать файлы, хранящиеся в Selection, и после сохранения окончательных данных в папке Final удалите все файлы внутри Selection, но не сами папки.

Вот код, я использую для файлов внутри выбора

for the_file in os.listdir(claendir): 
    file_path = os.path.join(claendir, the_file) 
    try: 
     if os.path.isfile(file_path): 
      os.unlink(file_path) 
    except Exception, e: 
     print e 

и вот весь код

import arcpy, shutil 
from arcpy import env 

# Set environment settings 
env.workspace = "C:\\GISData\\Data" 
InFeaturePath = "C:\\GISData\\Data\\" 
claendir='C:\\GISData\\Selection\\' 
shutil.rmtree(claendir); 
InFeatureName = "band-tailed_pigeon.shp" 
coulmn = "PataMN_1" 
OutFeatureName ="HornedLark.shp" 
InFeature = InFeaturePath + InFeatureName 
OutFeaturePath ="C:\\GISData\\Final\\" 
OutFeature = OutFeaturePath + OutFeatureName 
dropFields = ["id","WTRSHD_FID"] 

select_1 = "selected_1.shp" 
select_2 = "selected_2.shp" 
select_3 = "selected_3.shp" 
select_4 = "selected_4.shp" 
select_5 = "selected_5.shp" 
dissolve_1 = "dissolved_1.shp" 
dissolve_2 = "dissolved_2.shp" 
dissolve_3 = "dissolved_3.shp" 
dissolve_4 = "dissolved_4.shp" 
dissolve_5 = "dissolved_5.shp" 


# Process: Select 
# Select 1 
arcpy.Select_analysis(InFeature, select_1, coulmn+ " <= 0.200000") 
# Select 2 
arcpy.Select_analysis(InFeature, select_2, coulmn+ ">= 0.200001 AND " +coulmn+ " <= 0.400000") 
# Select 3 
arcpy.Select_analysis(InFeature, select_3, coulmn+ ">= 0.400001 AND " +coulmn+ " <= 0.600000") 
# Select 4 
arcpy.Select_analysis(InFeature, select_4, coulmn+ ">= 0.600001 AND " +coulmn+ " <= 0.800000") 
# Select 5 
arcpy.Select_analysis(InFeature, select_5, coulmn+ ">= 0.800001 AND " +coulmn+ " <= 1") 


# Process: Dissolve 
# Dissolve 1 
arcpy.Dissolve_management(select_1, dissolve_1, "", "", "MULTI_PART", "DISSOLVE_LINES") 
print "Disolve 1 Finished" 
# Dissolve 2 
arcpy.Dissolve_management(select_2, dissolve_2, "", "", "MULTI_PART", "DISSOLVE_LINES") 
print "Disolve 2 Finished" 
# Dissolve 3 
arcpy.Dissolve_management(select_3, dissolve_3, "", "", "MULTI_PART", "DISSOLVE_LINES") 
print "Disolve 3 Finished" 
# Dissolve 4 
arcpy.Dissolve_management(select_4, dissolve_4, "", "", "MULTI_PART", "DISSOLVE_LINES") 
print "Disolve 4 Finished" 
# Dissolve 5 
arcpy.Dissolve_management(select_5, dissolve_5, "", "", "MULTI_PART", "DISSOLVE_LINES") 
print "Disolve 5 Finished" 

print "Add Field Started ...." 
# Process: Add Field 
# Add Field 1 
arcpy.AddField_management(dissolve_1, "type", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") 
# Add Field 2 
arcpy.AddField_management(dissolve_2, "type", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") 
# Add Field 3 
arcpy.AddField_management(dissolve_3, "type", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") 
# Add Field 4 
arcpy.AddField_management(dissolve_4, "type", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") 
# Add Field 5 
arcpy.AddField_management(dissolve_5, "type", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") 

print "Add Field End " 
print "Calculate Field Started ...." 
# Process: Calculate Field 
# Calculate Field 1 
arcpy.CalculateField_management(dissolve_1, "type", "1", "VB", "") 
# Calculate Field 2 
arcpy.CalculateField_management(dissolve_2, "type", "2", "VB", "") 
# Calculate Field 3 
arcpy.CalculateField_management(dissolve_3, "type", "3", "VB", "") 
# Calculate Field 4 
arcpy.CalculateField_management(dissolve_4, "type", "4", "VB", "") 
# Calculate Field 5 
arcpy.CalculateField_management(dissolve_5, "type", "5", "VB", "") 
print "Calculate Field End" 
print "Delete Field Started ...." 
# Process: Delete Extra Field 
# Delete Field 1 
arcpy.DeleteField_management(dissolve_1, dropFields) 
# Delete Field 2 
arcpy.DeleteField_management(dissolve_2, dropFields) 
# Delete Field 3 
arcpy.DeleteField_management(dissolve_3, dropFields) 
# Delete Field 4 
arcpy.DeleteField_management(dissolve_4, dropFields) 
# Delete Field 5 
arcpy.DeleteField_management(dissolve_5, dropFields) 
print "Delete Field End" 

print "Merge Started ...." 
# Process: Merge 
inFeaturesToMerge = [dissolve_1, dissolve_2, dissolve_3, dissolve_4, dissolve_5] 
arcpy.Merge_management(inFeaturesToMerge, OutFeature) 

for the_file in os.listdir(claendir): 
    file_path = os.path.join(claendir, the_file) 
    try: 
     if os.path.isfile(file_path): 
      os.unlink(file_path) 
    except Exception, e: 
     print e 

Еще раз необходимо удалить только файлы внутри Selection (а не папку) и сделать это после завершения всего анализа.

ответ

2

Вы это на 8-ой строке вашего полного кода:

shutil.rmtree(claendir); 

Это удаляет claendir и все под ним.

Смежные вопросы