删除文件夹中的文件,但不删除文件夹

时间:2015-09-04 21:54:33

标签: python

我在删除名为Selection

的文件夹中的文件时遇到两个问题
  1. 该脚本假设只删除Selection文件夹中的文件,但它也会删除Selection文件夹。

  2. Selection文件夹在脚本运行的最初阶段已被删除,我将该过程放在最后。实际上我需要分析存储在Selection中的文件,并在将最终数据存储到Final文件夹后删除Selection内的所有文件,但不删除文件夹本身。

  3. 以下是我用于选择内的文件的代码

    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文件夹(而不是文件夹)中的ONLY文件,并在完成所有分析后执行此操作。

1 个答案:

答案 0 :(得分:2)

你在完整代码的第8行有这个:

shutil.rmtree(claendir);

这会删除claendir及其下方的所有内容。