Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create def for sorted() from list of versioning files (filename+datetime)
#1
Hi everyone.

I am running FreeFileSync app on PC clients to sync/backup Users folders to network file server, with versioning turned on.

System is Windows 7 64-bit with Python 3.5.

Versioning does following: If synced file is new, then old file is moved into special folder, and then new file is synced/copied. Problem arises because FreeFileSync does not have option to limit the number of versions kept, so with large files (2GB+) that are changed daily like Outlook.pst, Thunderbird msf files, etc, HDD is filled in just few days/weeks.

So I decided to create a Python script that will go through Versioning locations/folders, separate/filter versions of each file, and delete any but just 2 last files. Just deleting anything older then (current date - x days) is not viable because if some file has not changed in that time, all versions (but current backup) will be deleted.

Now we come to the point where I am stuck. I have following format of file names in folders I need to parse:

Quote: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

Notice that versioning file name has original filename with ext, then space, then YYYY-MM-DD date, space, then HHmmss and then .ext of original file.

What I need to do is to create a "key" definition for sorted() to find and separate all files from separate original files (like archive.pst, Outlook.pst, Outlook.sharing.xml.obi)

I think best option to separate/recognize them is to recognize ext of the file from end of the file (.pst, .obi) and to locate that substring in the rest of the file. Then, using sublist of files belonging to original file, to sort them by datetime (newest first?) from filename (date of the file might be different then in filename if something goes wrong!), eliminate those two (first?) files from the list (copy the rest into new resulting list?) so I can delete all extra files (on that resulting? list) in next step, leaving only two latest files.

Problem is that I am new with Python and all of this is WAY above my understanding, and HDD is already pretty full so I am deleting huge files manually.

Code to replicate is like this:
def ljfilter(a):
# def code here
   return

MyDir = 'c:/test'
os.chdir(MyDir)

lista = [f for f in listdir(MyDir) if isfile(join(MyDir, f))]
for item in sorted(lista, key=ljfilter):
   print(item)

Quote
#2
Here are some hints.
You can probably use getmtime(path),
Return the time of last modification of path/file.
Eg:
from datetime import datetime
from glob import glob
from os.path import basename,getmtime,join
import os
 
des = 'C:/foo'
for f_name in glob(join(des, '*.pst')):
    file_name = basename(f_name)
    time_info = getmtime(f_name)
    date = datetime.fromtimestamp(time_info)
    print(file_name,date)
You can use timedelta and set i back 2 days,and use it the loop to compare dates.
If files are older than 2 days os.remove().
Eg:
>>> from datetime import datetime,timedelta
>>> today = datetime.today()
>>> two_days_ago = today - timedelta(2)
>>> today.day
11
>>> two_days_ago.day
9
Quote
#3
(Jan-11-2017, 06:09 PM)snippsat Wrote: Here are some hints.
You can probably use getmtime(path),
Return the time of last modification of path/file.
Thank you for your effort, but as I already wrote (I know I wrote large text, hard to follow):
Quote:Just deleting anything older then (current date - x days) is not viable because if some file has not changed in that time, all versions (but current backup) will be deleted.
Versioning files appear only if there was a change. If last change of certain file was a year ago, then deleting anything older then a month would also delete every single versioning file, leaving only same file as user/client has on his PC.

So to keep at least one versioning file, it is imperative to parse the text and pull out:
1. original filename
2. date and time from the name itself

Which answers I need, as far as I understand:
a) I need to sort the list of files based on original name and date time (string operations), newer first
b) Outside of the sorted() I need to scroll through sorted list and using same string tools from a) and leave/skip only first two for each original file, deleting the rest.
c) Can someone provide me at least with proper format of "def" definition (with return?) so I can use it as a base? I understood that I need to use "key" function. This is the hardest part to understand.
Quote
#4
(Jan-12-2017, 06:54 AM)DrLove73 Wrote: I understood that I need to use "key" function. This is the hardest part to understand.
You can use datetime.strptime to convert it to date format,the use it in sorted() with key.
Eg:
>>> from datetime import datetime
>>> from pprint import pprint

>>>
... data = '''\
... archive.pst 2016-10-1 080101.pst
... archive.pst 2016-10-20 080101.pst
... archive.pst 2016-10-8 080101.pst'''

>>> pst_files = []
>>> for line in data.split('\n'):
...     pst_files.append(line.split())

>>> pprint(pst_files)
[['archive.pst', '2016-10-1', '080101.pst'],
 ['archive.pst', '2016-10-20', '080101.pst'],
 ['archive.pst', '2016-10-8', '080101.pst']]


>>> pprint(sorted(pst_files, key=lambda x: datetime.strptime(x[1], "%Y-%m-%d"), reverse=True))
[['archive.pst', '2016-10-20', '080101.pst'],
 ['archive.pst', '2016-10-8', '080101.pst'],
 ['archive.pst', '2016-10-1', '080101.pst']]
Quote
#5
Ok, that is VERY helpful.

I improved on your code so that I remove extension at the end, and then get datetime string from both date and time.
I do have problem that I need to sort items in a list according to first column, and only then based on date.

Here is latest code:

# Is there a way to modify this to only give me only the newest MP3 file?

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("lista2 sorted:")
pprint(sorted(lista2, key=lambda x: datetime.strptime(x[1] + " " + x[2], "%Y-%m-%d %H%M%S"), reverse=True))
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', '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'],  ['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']] lista2 sorted: [['archive.pst', '2016-10-20', '080101'],  ['Outlook.pst', '2016-10-20', '080101'],  ['Outlook.sharing.xml.obi', '2016-10-20', '080101'],  ['archive.pst', '2016-10-19', '080101'],  ['Outlook.pst', '2016-10-19', '080101'],  ['Outlook.sharing.xml.obi', '2016-10-19', '080101'],  ['archive.pst', '2016-10-18', '080101'],  ['Outlook.pst', '2016-10-18', '080101'],  ['Outlook.sharing.xml.obi', '2016-10-18', '080101'],  ['archive.pst', '2016-10-17', '080101'],  ['Outlook.pst', '2016-10-17', '080101'],  ['Outlook.sharing.xml.obi', '2016-10-17', '080101'],  ['archive.pst', '2016-10-15', '080101'],  ['Outlook.pst', '2016-10-15', '080101'],  ['Outlook.sharing.xml.obi', '2016-10-15', '080101'],  ['archive.pst', '2016-10-14', '080101'],  ['Outlook.pst', '2016-10-14', '080101'],  ['Outlook.sharing.xml.obi', '2016-10-14', '080101']] Process finished with exit code 0

Ok, I resolved sorting with:
pprint(sorted(lista2, key=lambda x: (x[0], datetime.strptime(x[1] + " " + x[2], "%Y-%m-%d %H%M%S")), reverse=True))
Quote
#6
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:

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
Quote
#7
I have managed to fix last problem by not discarding ext string, but adding as last column in the list.

Here is code:
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]

# os.chdir("e:/Backup-racunara/Novi_Sad/Dole_napred/JASMINA-PC/Revizije/C/Users/jasmina/AppData/Local/Microsoft/Outlook/")
MyDir = 'c:/testasdasda'
os.chdir(MyDir)
maxfiles = 2

pst_files = []
lista2 = []
lista3 = []
deletelist = []

lista = [f for f in listdir(MyDir) if isfile(os.path.expanduser(fajl))]
# 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] + " " + os.path.splitext(line)[1]
    originalnamestring = os.path.splitext(line)[0]
    originalname = originalnamestring[:-19]
    timestring = originalnamestring[-7:-1]
    datestring = originalnamestring[-18:-8]
    lista2.append((originalname,datestring,timestring,os.path.splitext(line)[1]))
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)
print("+++++++++++++++++")

for item in deletelist:
    f = join(os.path.normpath(MyDir), item[0]+" "+item[1]+" "+item[2]+item[3])
    print("File: "+f+" - Exist: "+str(isfile(f)))
    os.remove(f)

My next problem is that it does not work for either very long file paths, or paths with space:

Quote:"e:/Backup-racunara/Novi_Sad/Dole_napred/JASMINA-PC/Revizije/C/Users/jasmina/AppData/Local/IconCache.db 2016-10-14 080101.db"
"e:/Backup-racunara/Novi_Sad/Dole_napred/JASMINA-PC/Revizije/C/Users/lj/NTUSER.DAT 2016-10-14 080101.DAT"

Any advice how to adapt this script for filename and directory names with spaces?
Quote
#8
I can not edit the previous post for some reason.

It turns out I have security/access issues with selected files, to I consider that problem solved

Next order of business is to expand the script to cycle through all subdirectories and cleans them one by one.
Quote
#9
(Jan-13-2017, 11:28 AM)DrLove73 Wrote: it does not work for either very long file paths, or paths with space:

Quote:"e:/Backup-racunara/Novi_Sad/Dole_napred/JASMINA-PC/Revizije/C/Users/jasmina/AppData/Local/IconCache.db 2016-10-14 080101.db"
"e:/Backup-racunara/Novi_Sad/Dole_napred/JASMINA-PC/Revizije/C/Users/lj/NTUSER.DAT 2016-10-14 080101.DAT"

Any advice how to adapt this script for filename and directory names with spaces?

Please be more specific, which error message do you see, or which unexpected output, which other would you expect?

Also, at this time, your code is rather difficult to review. Do you already know about python functions or maybe even classes to help bring more structure and readability to the code?

This way, you can also easier update and improve some portions of the code and test them separately. You may also be thankful in a few months or years when you come back and try to understand what you coded. ;)
Quote
#10
(Jan-13-2017, 01:27 PM)Kebap Wrote: Please be more specific, which error message do you see, or which unexpected output, which other would you expect?

I had/have file permission issues, because even though I am Domain Admin, majority of files have only Original Owner, SYSTEM and PC\Administrators group in file permissions (Where PC is PC where backup files are stored).
Manifestation is that I get empty file list "lista", so there is nothing to do.
Output:
Exists dir: True Directory name lenght: 108 Directory is: e:/Backup-racunara/Novi_Sad/Dole_napred/JASMINA-PC/Revizije/C/Users/jasmina/AppData/Local/Microsoft/Windows/ lista: [] lista2: [] lista3 sorted: [] .................. deletelist: [] +++++++++++++++++ Process finished with exit code 0
I will either have to add my user/Domain Admins group full rights for every file on the server,
or to run this script as PC\Administrators member, or SYSTEM account.

EDIT: I decided to just run whole script as Administrator of that PC. Running Pycharm as Administrator allows me to debug so I will not be complicating python script.



(Jan-13-2017, 01:27 PM)Kebap Wrote:
Quote:Also, at this time, your code is rather difficult to review. Do you already know about python functions or maybe even classes to help bring more structure and readability to the code?

This way, you can also easier update and improve some portions of the code and test them separately. You may also be thankful in a few months or years when you come back and try to understand what you coded. ;)

I haven't had the time to finish reading book about Python, but I know Delphi/pascal and VB so I know about functions in general.

I plan to clean this code as soon as everything works as planed. Since I need to cycle through directories, I will be employing functions at least for that.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Create a new list dynamically floatingshed 6 95 Nov-20-2017, 01:25 PM
Last Post: floatingshed
  try/except for filename extensions? PythonAndArduino 1 68 Nov-13-2017, 02:04 AM
Last Post: snippsat
  Create list from String purnima1 3 177 Oct-02-2017, 05:21 AM
Last Post: purnima1
  Create class instances from list of strings pythonck 1 91 Sep-18-2017, 06:13 PM
Last Post: ichabod801
  Convert grib files to text files tuxman 6 253 Sep-15-2017, 03:26 PM
Last Post: nilamo
  dynamically create a list wfsteadman 0 86 Aug-30-2017, 05:34 PM
Last Post: wfsteadman
  read multiple .xlsx files and text files in a directory BNB 11 1,044 Jun-07-2017, 07:42 AM
Last Post: BNB
  Subtract Minutes from Datetime.Time tkj80 2 755 May-11-2017, 09:56 AM
Last Post: klllmmm
  Convert String to Datetime Object tkj80 2 621 Apr-20-2017, 08:45 AM
Last Post: volcano63
  Error when converting MATLAB's datenum to Python's datetime zoya2385 5 735 Apr-03-2017, 06:01 PM
Last Post: zivoni

Forum Jump:


Users browsing this thread: 1 Guest(s)