0

i am new to python. I hava a task to clean up all the old video files that are 12 months old from our Common Drive.

The folder structure is same for every folder(Events) with subfolders 'Movies' and 'Sorted'. There are atleast 1000 such Event Folders with avi and raw (video) files in their subfolders 'Movies'.

I have to move all the avi files to 'Sorted\AVI_Sort' Folder and raw files to 'Sorted\RAW_Sort' for each of these 1000 Events. and then make a zip of the Event_Name folder, store in Sorted folder and delete the Original Event_Name folder.

I have written the following code but it works only partly . finding the 12 months old file does not work and removing original file after zipping i get a Error message 'permission denied'. What am i doing wrong here?

from os import path
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
import os, sys
import os.path
import shutil

#function to find 12 months old file

def find_old_files(file):
  twelve_months_ago = datetime.now() - relativedelta(months=12)
  filetime = datetime.fromtimestamp(path.getctime(file))
  if filetime < twelve_months_ago:
     return file

#move avi and raw files in Sorted folder

ext = ("avi", "AVI", "Avi")
for i,j,y in os.walk(os.getcwd()):
   if i.endswith('Sorted'):
      dest = i
   if i.endswith('Movies'):
      dest_raw = dest +  '\\RAW_Sort'
      dest_avi = dest +  '\\AVI_Sort'
      files = os.listdir(i)
      for f in files:
          if find_old_files(f): # function to find 12 months old file
                if not f.endswith(ext):
                    shutil.move(i+'\\'+f, dest_raw)
                if f.endswith(ext):
                    shutil.move(i+'\\'+f, dest_avi)

#zip and delete the original file

for i,j,y in os.walk(os.getcwd()):
   if i.endswith('_Sort'):
      shutil.make_archive(os.path.basename(i), 'zip', i)
      os.remove(os.path.dirname(i))
deadshot
  • 8,881
  • 4
  • 20
  • 39
  • this will help [How to get file creation & modification date/times in Python](https://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python) – deadshot Sep 11 '20 at 11:33
  • 1
    instead of checking with three `("avi", "AVI", "Avi")` you can just convert the sting to lower and then check – deadshot Sep 11 '20 at 11:36

0 Answers0