3

I did a lot of geoprocessing steps and now I like to delete all files including result, result, _join, _interand reclass in their name. I can do this with the following

import glob
import os
for fl in glob.glob(r"D:\Users\julia\erste_aufg\delete\*result*" ):
    os.remove(fl)

for f2 in glob.glob(r"D:\Users\julia\erste_aufg\delete\*result*" ):
    os.remove(f2)

for f3 in glob.glob(r"D:\Users\julia\erste_aufg\delete\*_join*" ):
    os.remove(f3)

for f4 in glob.glob(r"D:\Users\julia\erste_aufg\delete\*_inter*" ):
    os.remove(f4)

for f5 in glob.glob(r"D:\Users\julia\erste_aufg\delete\*reclass*" ):
    os.remove(f5)

see here.

But before deleting I created a resultfolder in the folder(workspace) with the files and copied the results of the geoprocessing steps which I need furthermore to this resultfolder. Because of this resultfolder the code to delete is not working any more. What can I do that the delete-code disregards the resultfolder?

(The code down is just to make the question more clear)

import arcpy
import os
from arcpy import env
env.workspace = r"env.workspace = r"D:\Users\julia\urban_A_sel""
#new folder
# Set local variables
out_folder_path = env.workspace
out_name = "resultfolder"

# Execute CreateFolder
arcpy.CreateFolder_management(out_folder_path, out_name)
newpath= env.workspace + "\\" + out_name
out_gdb_path = newpath
arcpy.CreateFileGDB_management(out_gdb_path, "output.gdb")
outWorkspace= newpath +"\\"+"output.gdb"
resultList = arcpy.ListFeatureClasses("*_result*")
arcpy.FeatureClassToGeodatabase_conversion(resultList, outWorkspace) 
  • 1
    Could you change the output folder's name to something your delete script wouldn't be interested in -- like out_name = "datafolder"? – Erica Oct 10 '14 at 11:36
  • 1
    Could you clear up what the structure of the files is. I.e. provide an example of say five files in a couple of directories and highlight which you want to keep and which you want to remove. – GIS-Jonathan Oct 10 '14 at 13:22
2

If I understand you correctly, you could use os.walk and remove the folder from dirnames. Python docs show an example of this, similar to what fluidmotion has but within the traversing itself.

import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
    print root, "consumes",
    print sum(getsize(join(root, name)) for name in files),
    print "bytes in", len(files), "non-directory files"
    if 'CVS' in dirs:
        dirs.remove('CVS')  # don't visit CVS directories
  • Thanks for your answer. I just had to change the name of the “resultfolder” as Erica said. – Nora Oct 13 '14 at 9:24
1

maybe a bit clunky, but it seems you could try something like

if os.path.split(os.path.dirname(f1))[-1]!='resultfolder':
    os.remove(f1)

so that after glob returns a full path, this would strip off the last directory of the path and compare it with 'resultfolder' - if the directory is different, it would remove the file

  • Thanks for your answers. I just had to change the name of the “resultfolder” as Erica said. – Nora Oct 13 '14 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.