0

I'm trying to get the change time of a set of files in a folder. Everytime any archive changes, I need to autocreate a backup copy of the archives that have been last modified.

My tests:

I have tried the following code to get the date/time of chages and i need to list it to compare.

import os
import shutil
from pathlib import Path
import os.path, time


class edit:
    def __init__(last_edit, chage, creation):
        last_edit.change = list
        last_edit.creation = list


file_soure = 'C:/Users/Automation/Desktop/NEW2/'
#file_destination = 'C:/Users/Automation/Desktop/NEW/'

get_files = os.listdir(file_source)

for x in range(2,len(get_files)):
    last_edit.change = time.ctime(os.path.getmtime(x + get_files))


print(last_edit.change)

for g in get_files:
    shutil.copy(file_source + g, file_destination)

Thats the result:

['Teste.txt', 'Teste2.txt']
Traceback (most recent call last):
  File "c:\Users\Automação\Desktop\Scripts\Python\Teste.py", line 29, in <module>
    print(last_edit.change)
NameError: name 'last_edit' is not defined
PS C:\Users\Automação\Desktop\Scripts\Python> 

The object isnt declared?

  • do you think `last_edit.change = time.ctime(os.path.getmtime(x + get_files))` is in __init__ method? – ansev Dec 07 '22 at 12:18
  • You will need to compare a file's most recent update time to its previous update time. There doesn't appear to be any attempt to do this in your code – DarkKnight Dec 07 '22 at 12:25
  • maybe duplication: https://stackoverflow.com/questions/375154/how-do-i-get-the-time-a-file-was-last-modified-in-python – Sezer BOZKIR Dec 07 '22 at 12:28
  • @Cobra Yep, I'm building from the start. First I need to get the data to a list, and then i can compare. I'm struggling with getting the data from the list or files.. – Marcos.MAlmeida Dec 07 '22 at 12:31
  • @Marcos.MAlmeida, taking a quick look at your code, it seems to have a number of flaws: first of all, the variable last_edit is never initialized. There no line of code starting with `last_edit = `. Secondly, you are defining a class `edit` that you are not using. Are you sure you want to use class objects here? In general: maybe you should try to write down in text how you want to implement this, because it is not really clear from the code... – QuadU Dec 07 '22 at 13:34

1 Answers1

0

I just found what i needed to do.

import os
import shutil
import filecmp
from datetime import datetime
try:    
    file_source = 'D:/00-Backup/99. Temp/'
    file_comparator = 'D:/00-Backup/01. PLC/'
    get_files = os.listdir(file_source)
    cmp_files = os.listdir(file_comparator)
    os_date = datetime.now().strftime('%Y-%m-%d')
    plc_name = "PLC1"
    common = get_files
 
    match,missmatch,errors = filecmp.cmpfiles(file_source, file_comparator,common, shallow=True)

    print(match)
    print(missmatch)
    print(errors)

    if errors[0] in get_files:
        os.makedirs('D:/00-Backup/01. PLC/'+ plc_name + "-" + "%s" % os_date)
        file_destination = 'D:/00-Backup/01. PLC/'+ plc_name + "-" + "%s" % os_date
        for g in get_files:
            shutil.copy(file_source + g, file_destination)
    elif missmatch in get_files:
        os.makedirs('D:/00-Backup/01. PLC/'+ plc_name + "-" + "%s" % os_date)
        file_destination = 'D:/00-Backup/01. PLC/'+ plc_name + "-" + "%s" % os_date
        for g in get_files:
            shutil.copy(file_source + g, file_destination)
except IndexError:
    pass