I have a new problem.
I have a list of files that need to be deleted ("deletelist"), but to be able to delete them from filesystem, I need to locate them in actual file list ("lista"), adding directory path in front because I trimmed file extension in all latter lists for convinience.
Here is working code, problem starts at like 46:
I have a list of files that need to be deleted ("deletelist"), but to be able to delete them from filesystem, I need to locate them in actual file list ("lista"), adding directory path in front because I trimmed file extension in all latter lists for convinience.
Here is working code, problem starts at like 46:
import os from os import listdir from os.path import isfile, join from datetime import datetime from pprint import pprint def removeext(path): return os.path.splitext(path)[0] MyDir = 'c:/test' #os.chdir(MyDir) pst_files = [] ista2 = [] #lista = [f for f in listdir(MyDir) if isfile(join(MyDir, f))] lista = ['archive.pst 2016-10-14 080101.pst', 'archive.pst 2016-10-15 080101.pst', 'archive.pst 2016-10-17 080101.pst', 'archive.pst 2016-10-18 080101.pst', 'archive.pst 2016-10-19 080101.pst', 'archive.pst 2016-10-20 080101.pst', 'Outlook.pst 2016-10-14 080101.pst', 'Outlook.pst 2016-10-15 080101.pst', 'Outlook.pst 2016-10-17 080101.pst', 'Outlook.pst 2016-10-18 080101.pst', 'Outlook.pst 2016-10-19 080101.pst', 'Outlook.pst 2016-10-20 080101.pst', 'Outlook.sharing.xml.obi 2016-10-14 080101.obi', 'Outlook.sharing.xml.obi 2016-10-15 080101.obi', 'Outlook.sharing.xml.obi 2016-10-17 080101.obi', 'Outlook.sharing.xml.obi 2016-10-18 080101.obi', 'Outlook.sharing.xml.obi 2016-10-19 080101.obi', 'Outlook.sharing.xml.obi 2016-10-20 080101.obi'] print("lista:") print(lista) for line in lista: line = os.path.splitext(line)[0] lista2.append(line.split()) print("lista2:") pprint(lista2) print("lista3 sorted:") list3 = sorted(lista2, key=lambda x: (x[0], datetime.strptime(x[1] + " " + x[2], "%Y-%m-%d %H%M%S")), reverse=True) pprint(list3) print("..................") originalname = "" nofiles = 1 for item in list3: if item[0] != originalname: originalname = item[0] nofiles = 1 if nofiles > maxfiles: print( str(nofiles) + ": " + originalname + " " + item[1]) deletelist.append(item) nofiles = nofiles + 1 print("deletelist:") pprint(deletelist) # Problematic code, I need to locate item in the list that consists of item[0]+" "+item[1]+" "+item[2] and then actually delete that file for item in deletelist: f = join(os.path.normpath(MyDir), item[0]+" "+item[1]+" "+item[2]) sub = str(item[0] + " " + item[1] + " " + item[2]) print("f: "+f) print("sub: "+sub) print("isfile(f): "+str(isfile(f))) print("test") # locate item in "lista" list that has "sub" in it #print(join(f for f in list if sub in f))
Output:lista:
['archive.pst 2016-10-14 080101.pst', 'archive.pst 2016-10-15 080101.pst', 'archive.pst 2016-10-17 080101.pst', 'archive.pst 2016-10-18 080101.pst', 'archive.pst 2016-10-19 080101.pst', 'archive.pst 2016-10-20 080101.pst', 'archive.pst 2016-10-20 080501.pst', 'Outlook.pst 2016-10-14 080101.pst', 'Outlook.pst 2016-10-15 080101.pst', 'Outlook.pst 2016-10-17 080101.pst', 'Outlook.pst 2016-10-18 080101.pst', 'Outlook.pst 2016-10-19 080101.pst', 'Outlook.pst 2016-10-20 080101.pst', 'Outlook.sharing.xml.obi 2016-10-14 080101.obi', 'Outlook.sharing.xml.obi 2016-10-15 080101.obi', 'Outlook.sharing.xml.obi 2016-10-17 080101.obi', 'Outlook.sharing.xml.obi 2016-10-18 080101.obi', 'Outlook.sharing.xml.obi 2016-10-19 080101.obi', 'Outlook.sharing.xml.obi 2016-10-20 080101.obi']
lista2:
[['archive.pst', '2016-10-14', '080101'],
['archive.pst', '2016-10-15', '080101'],
['archive.pst', '2016-10-17', '080101'],
['archive.pst', '2016-10-18', '080101'],
['archive.pst', '2016-10-19', '080101'],
['archive.pst', '2016-10-20', '080101'],
['archive.pst', '2016-10-20', '080501'],
['Outlook.pst', '2016-10-14', '080101'],
['Outlook.pst', '2016-10-15', '080101'],
['Outlook.pst', '2016-10-17', '080101'],
['Outlook.pst', '2016-10-18', '080101'],
['Outlook.pst', '2016-10-19', '080101'],
['Outlook.pst', '2016-10-20', '080101'],
['Outlook.sharing.xml.obi', '2016-10-14', '080101'],
['Outlook.sharing.xml.obi', '2016-10-15', '080101'],
['Outlook.sharing.xml.obi', '2016-10-17', '080101'],
['Outlook.sharing.xml.obi', '2016-10-18', '080101'],
['Outlook.sharing.xml.obi', '2016-10-19', '080101'],
['Outlook.sharing.xml.obi', '2016-10-20', '080101']]
lista3 sorted:
[['archive.pst', '2016-10-20', '080501'],
['archive.pst', '2016-10-20', '080101'],
['archive.pst', '2016-10-19', '080101'],
['archive.pst', '2016-10-18', '080101'],
['archive.pst', '2016-10-17', '080101'],
['archive.pst', '2016-10-15', '080101'],
['archive.pst', '2016-10-14', '080101'],
['Outlook.sharing.xml.obi', '2016-10-20', '080101'],
['Outlook.sharing.xml.obi', '2016-10-19', '080101'],
['Outlook.sharing.xml.obi', '2016-10-18', '080101'],
['Outlook.sharing.xml.obi', '2016-10-17', '080101'],
['Outlook.sharing.xml.obi', '2016-10-15', '080101'],
['Outlook.sharing.xml.obi', '2016-10-14', '080101'],
['Outlook.pst', '2016-10-20', '080101'],
['Outlook.pst', '2016-10-19', '080101'],
['Outlook.pst', '2016-10-18', '080101'],
['Outlook.pst', '2016-10-17', '080101'],
['Outlook.pst', '2016-10-15', '080101'],
['Outlook.pst', '2016-10-14', '080101']]
..................
3: archive.pst 2016-10-19
4: archive.pst 2016-10-18
5: archive.pst 2016-10-17
6: archive.pst 2016-10-15
7: archive.pst 2016-10-14
3: Outlook.sharing.xml.obi 2016-10-18
4: Outlook.sharing.xml.obi 2016-10-17
5: Outlook.sharing.xml.obi 2016-10-15
6: Outlook.sharing.xml.obi 2016-10-14
3: Outlook.pst 2016-10-18
4: Outlook.pst 2016-10-17
5: Outlook.pst 2016-10-15
6: Outlook.pst 2016-10-14
deletelist:
[['archive.pst', '2016-10-19', '080101'],
['archive.pst', '2016-10-18', '080101'],
['archive.pst', '2016-10-17', '080101'],
['archive.pst', '2016-10-15', '080101'],
['archive.pst', '2016-10-14', '080101'],
['Outlook.sharing.xml.obi', '2016-10-18', '080101'],
['Outlook.sharing.xml.obi', '2016-10-17', '080101'],
['Outlook.sharing.xml.obi', '2016-10-15', '080101'],
['Outlook.sharing.xml.obi', '2016-10-14', '080101'],
['Outlook.pst', '2016-10-18', '080101'],
['Outlook.pst', '2016-10-17', '080101'],
['Outlook.pst', '2016-10-15', '080101'],
['Outlook.pst', '2016-10-14', '080101']]
f: c:\test\archive.pst 2016-10-19 080101
sub: archive.pst 2016-10-19 080101
isfile(f): False
test
f: c:\test\archive.pst 2016-10-18 080101
sub: archive.pst 2016-10-18 080101
isfile(f): False
test
f: c:\test\archive.pst 2016-10-17 080101
sub: archive.pst 2016-10-17 080101
isfile(f): False
test
f: c:\test\archive.pst 2016-10-15 080101
sub: archive.pst 2016-10-15 080101
isfile(f): False
test
f: c:\test\archive.pst 2016-10-14 080101
sub: archive.pst 2016-10-14 080101
isfile(f): False
test
f: c:\test\Outlook.sharing.xml.obi 2016-10-18 080101
sub: Outlook.sharing.xml.obi 2016-10-18 080101
isfile(f): False
test
f: c:\test\Outlook.sharing.xml.obi 2016-10-17 080101
sub: Outlook.sharing.xml.obi 2016-10-17 080101
isfile(f): False
test
f: c:\test\Outlook.sharing.xml.obi 2016-10-15 080101
sub: Outlook.sharing.xml.obi 2016-10-15 080101
isfile(f): False
test
f: c:\test\Outlook.sharing.xml.obi 2016-10-14 080101
sub: Outlook.sharing.xml.obi 2016-10-14 080101
isfile(f): False
test
f: c:\test\Outlook.pst 2016-10-18 080101
sub: Outlook.pst 2016-10-18 080101
isfile(f): False
test
f: c:\test\Outlook.pst 2016-10-17 080101
sub: Outlook.pst 2016-10-17 080101
isfile(f): False
test
f: c:\test\Outlook.pst 2016-10-15 080101
sub: Outlook.pst 2016-10-15 080101
isfile(f): False
test
f: c:\test\Outlook.pst 2016-10-14 080101
sub: Outlook.pst 2016-10-14 080101
isfile(f): False
test
Process finished with exit code 0