Home > Blockchain >  Check file modification date and copy it to target direction and copy only those that are modified i
Check file modification date and copy it to target direction and copy only those that are modified i

Time:02-05

So, I have this code

target_dir = "/mnt/..."

for source_dir in glob.iglob("/mnt/.../*bla.png"):
      file_date=(os.path.getmtime(source_dir))
      d=datetime.datetime(2022,2,3)
      if file_date>= d and file_date < (d datetime.timedelta(days=1)):
        shutil.copy(source_dir, target_dir)

but when I'm trying to run it, It gives me this error

File "a.py", line 33, in <module>
if file_date > d and file_date < (d datetime.timedelta(days=1)):
TypeError: '>' not supported between instances of 'float' and 'datetime.datetime'

I'm stuck and i don't know what to do. Any suggestions?

CodePudding user response:

os.path.getmtime() returns a float, while datetime.datetime() returns a datetime instance and that's why you are getting the error. One solution is to convert the float to datetime using the fromtimestamp method:

file_date = datetime.datetime.fromtimestamp(os.path.getmtime(source_dir))
  •  Tags:  
  • Related