diracfiles = [] with open('lhcb-user-e-egraveri.lfns', 'rb') as infile: for line in infile: diracfiles.append(str(line).strip()) savefrom_1 = 124829 -1 saveuntil_1 = 124837 savefrom_2 = 124958 saveuntil_2 = 124966 #myrange = range(savefrom_1, saveuntil_1+1) + range(savefrom_2, saveuntil_2) myrange = range(savefrom_2, saveuntil_2) for i in myrange: print i with open('files-to-delete.lfns', 'wb') as ofile: for df in diracfiles: delete = True #if '2016' in df: # delete = False for i in myrange: if ('2016_01/%s/' % i in df): delete = False if 'LDSB.' in df: delete = True if delete: print "EXTERMINATE: %s" % df ofile.write('%s\n' % df)