Pythonスクリプトは1日以上のファイルを削除するには、空のサブディレクトリを再帰的に削除し、レコードのログファイルを作成します!



誰が繰り返しの手動タスクを自動化するように、右か?しかし、私たちの心には、なぜこの特定のファイルとフォルダーの削除プロセスを自動化する必要があるの質問が来る!そして、我々が得る利益は何ですか!

心配しないで、私はあなたのための答えを持っている!

It is often necessary to delete files within a directory that are older than a particular number of days. This is especially the case with archive directories. Without performing routine maintenance, these archives can begin consuming large amounts of disk space in the server or your local machine . Cleaning file system regularly and manually seems time consuming and painful. Creating a script to remove older archives makes the maintenance process fast and painless.


ここではPythonは私たちの生活を容易にしています.Pythonはスクリプト用の優れたプログラミング言語です.あなたは別のオペレーティングシステム、すなわちWindows、Linux、UbuntuやIOSのための同じコードを使用することができます.

だから、我々のスクリプトにジャンプすることができます!
この自動化処理では、ディレクトリ内のすべてのファイルとフォルダを再帰的に一覧表示します.そして、それらの中で、古いファイルと空のサブディレクトリも同様に削除される必要があります.最後にではなく、少なくともログファイルを作成し、この実行の記録を保持します.

最初に考慮する必要条件はここにあります.
  1. Create a script which will delete files and sub folders in a directory recursively which are older than a day
  2. A list of each file inside the directory before delete and put them in a log file
  3. A list of all subfolders inside the directory before delete and put them in a log file
  4. If a subfolder has a recent file, do not delete the folder. Delete the older files only.
  5. But if a subfolder is empty, then delete the subfolder.
  6. Keep the records of deletion in the log file and scripts execution and stop date time.
  7. Log has to be a rolling log like everyday it creates a new log with date (Not appended to single log file)


この問題を解決するためにtodoを破壊しましょう.
  1. We need to input a directory where we will run our script
  2. We need to input a directory where we will save our log file
  3. Create a log file in the given directory ( in step 2 ) and name it uniquely with timestamp.
  4. Inside the given directory we will search all files and folders recursively.
  5. Get the latest modification date of each file and compare with preferable date.
  6. Check how older the files are and if the files are older than one day, we need to delete those.
  7. Check whether subfolders or subdirectories are empty or not. If any of the subfolders are empty then we also need to delete it.
  8. Finally, we need to keep the records of every files, folders, some metadata and delete status in the log file.

...

今、私たちはスクリプトを書くために良いです.

Please go through all the comments in the script to understand every steps there!


# Python 3
import time
import os
from glob import glob, iglob
from pathlib import Path
import glob
import datetime 
from datetime import datetime
import sys


# Directory validation function
def valid_dir(dir):
    #Checking if the path exists or not
    if not os.path.exists(dir):

        #Get current directory where the script has been executed
        cwd = os.getcwd()

        #Create .txt file for log
        f = open(cwd+"/log_createdAt_"+str(datetime.now().timestamp())+".txt",'w')

        #Write in the .txt file
        f.write("* Script execution started at : " + str(currentDate) +"\n"+"\n")
        f.write("This is not a valid path!"+"\n"+"\n")
        f.write("* Script execution stopped at : " + str(currentDate) +"\n"+"\n")
        print("Please provide valid path ")

        #exit
        sys.exit(1)

    #Checking if it is directory or not
    if not os.path.isdir(dir):

        #Get current directory where the script has been executed
        cwd = os.getcwd()

        #Create .txt file for log
        f = open(cwd+"/log_createdAt_"+str(datetime.now().timestamp())+".txt",'w')

        #Write in the .txt file
        f.write("* Script execution started at : " + str(currentDate) +"\n"+"\n")
        f.write("This is not a valid directory path!"+"\n"+"\n")
        f.write("* Script execution stopped at : " + str(currentDate) +"\n"+"\n")
        print("Please provide directory path ")

        #exit
        sys.exit(2)

# Function to convert list into string 
def listToString(s): 

    # initialize an empty string
    str1 = " , " 

    # return string  
    return (str1.join(s))

# Function to list all files and folders recursively inside a directory 
def search_filesNFolders(root_dir,log_dir):

    #Date to compare with file modification date
    compareDate = datetime.today()

    #Iteration integer
    i = 0

    #Create .txt file for log
    f = open(log_dir+"/log_createdAt_"+str(datetime.now().timestamp())+".txt",'w')

    f.write("* Script execution started at : " + str(currentDate) +"\n"+"\n")
    f.write("* Script execution Directory : " + root_dir +"\n")
    f.write("* Log file Directory : " + log_dir+"\n"+"\n")

    f.write("* Date to check with how older the file is : " + str(compareDate)+"\n"+"\n")

    #Loop to search all files and folders in the given directory recursively
    for currentpath, folders, files in os.walk(root_dir):

        #currentpath.replace('\','/')
        f.write("* Current path : "+ currentpath)
        f.write("\n")
        #currentpath.replace('\','/')

        #Iteration integer
        i = 0
        i = i+1

        #Check whether there are any folders in each path or not i.e length of folders list
        #Here there are no folders inside the current directory
        if(len(folders) == 0):

            #Writing the number of files and folders in the log file
            f.write("   Number of Folders : 0"+"\n")
            f.write("   Number of Files: " + str(len(files))+"\n")

            #Check whether there are any files in each folders in the same directory or not i.e length of files list
            if(len(files)==0):

                #Delete the subfolder as it is empty, No files and No folders inside
                os.rmdir(currentpath)

                f.write("   Note: This empty directory has been deleted!"+"\n")
            else:
                f.write("   Filenames: "+"\n")
            print("Folders : 0")

        #Here there are subfolders inside the current directory
        else:
            f.write("   Number of Folders: " + str(len(folders))+"\n")
            f.write("   Foldernames: " + listToString(folders)+"\n")
            f.write("   Number of Files: " + str(len(files))+"\n")

            #If there are files inside the current directory 
            if(len(files)!=0):
                f.write("   Filenames: "+"\n")

            print(folders)

        #Loop to get the metadata and check each file inside current directory
        for file in files:

            #Get the modification time of each file
            t = os.stat(os.path.join(currentpath,file))[8]

            #Check how older the file is from compareDate
            filetime = datetime.fromtimestamp(t) - compareDate
            print(filetime.days)

            #Log the record of file modification date time
            f.write("       "+str(i)+". "+file +"\n"+"          Modifiction date :"+str(datetime.fromtimestamp(t))+"\n"+"          File path : " +currentpath+"/"+file+ "\n")

            i = i+1

            #Check if file is older than 1 day
            if filetime.days < -1:

                #Remove the file
                os.remove(currentpath+"/"+file)

                #Write the delete status in log file
                f.write("       Note: This file has been deleted!"+"\n"+"\n")
                print('Deleted')
            else:
                print('Not older than 1 day!')
            print(file)

        f.write("\n"+"\n")

    #Execution stopped time recorded in log file
    f.write("* Script execution stopped at : " + str(datetime.today().strftime("%Y-%m-%d %H:%M:%S")) +"\n"+"\n")


if __name__=="__main__":

    #Define the directory where you want to run this script
    #root_dir = 'C:/Users/Zeaul.Shuvo/Music/Test'
    root_dir = input("Enter the directory path here for script execution - ")


    #Define the directory where you want to log the records
    #log_dir = 'C:/Users/Zeaul.Shuvo/Music'
    log_dir = input("Enter the directory path here for log file - ")

    #Current date
    currentDate = datetime.today().strftime("%Y-%m-%d %H:%M:%S")

    #Calling the function to validate the root directory
    valid_dir(root_dir)

    #Calling the function to validate the log file directory
    valid_dir(log_dir)

    #Calling the function to search files and folders, delete the older files and empty folders and keep record in log file. 
    search_filesNFolders(root_dir,log_dir)

    #exit
    sys.exit(4)

...

さあ?


冗談だ!
...
...

ここでは、ログファイル(. txtファイル)のスクリーンショットを実行します.

Remember : We need to keep the script execution start time and stop time in the log file. In every case, we need to keep this time records in log file. This helps us to track the activity of script execution.



我々は完了です!ハッピースクリプト!

任意の質問や提案を歓迎します.あなたの貴重な言葉を待って!ありがとう!