In this tutorial, we are going to learn about how to automate the process of removing files in a specific folder using python. We are going to use different packages but mainly our programming logic will help us.
No matter which kind of OS you are using, Windows, Mac, or Linux, you always will be worried about space. Nowadays 1 TB hard disk looks smaller. Talking about myself, I am a windows user and I have 128 GB SSD as a C drive. But I can barely use 25 GB. So, there might be some unnecessary files that I download throughout the week, and if we clean them then we might get rid of useless files. Though, this task seems boring. Yes, none of us want to do it. It takes time as we need to check the date on each one and them select and remove it.
That’s where our python comes to the picture. Python is a very useful language to automate this kind of boring stuff. What we need the right mindset, python IDE and some time to write and setup script. So, in this tutorial, we will try to write a script that removes the files which are older than a week from the specified path and also put it into output logs.
Let’s Get The General Idea First
The basic approach is we are going to enter the path and python will give us a file list in that directory. Then, we are going to check if the file is more than 7 days older. if yes then delete and put the entry in the log file. But, how we will get a file list? We can fetch that using os package. Os package gives us basic functionality which we can see by the right click of the mouse such as copy, rename, move, delete, etc.
Let’s start working on the script
import os import datetime import glob path = 'enter_path_here' today = datetime.datetime.today()#gets current time os.chdir(path) #changing path to current path(same as cd command) #we are taking current folder, directory and files #separetly using os.walk function for root,directories,files in os.walk(path,topdown=False): for name in files: #this is the last modified time t = os.stat(os.path.join(root, name)) filetime = datetime.datetime.fromtimestamp(t) - today #checking if file is more than 7 days old #or not if yes then remove them if filetime.days <= -7: print(os.path.join(root, name), filetime.days) os.remove(os.path.join(root, name))
First, we are going to remove files without worrying about logging. Then, we will write details in log files. Here, we need two libraries First one is os as mentioned, and the second one is datetime. After importing we stored the path and current date in the variable. We used the os.walk() function for traversing around the file tree. os.walk() gives also a file that is under sub-directory and we surely want to remove that.
So, in that loop, we check every file using the os.stat() function. os.stat provides us basic info/metadata about the file such as last modification time, permission, and so on. So, by using that, we stored the last modification time and stored it into the variable. But there is a small problem, we got that value in seconds and we need to convert it into the date. That’s when datetime package will help. Finally, we compare if the difference between both of them more than 7 days then we print the full file path, the difference in days, and remove that file using the os.remove() function.
Let’s Make a Log File
This is typically an operation you run automatically, but things may go wrong or you may want to audit the files you have deleted. This is where keeping a track of the deletions in a log file will help. For making a log file, you just need to know the basics of file handling using python and you can do it by yourself. Let me help you with that. We will modify our script with a few small changes by creaing a simple log file.
import os import datetime import glob path = #'enter your path here' logging_path= #'Enter your log directory path here' #making a log directory at given path(if exists then it will skip) if not os.path.isdir(logging_path): os.mkdir(logging_path) else: print("Directory already exists") today = datetime.datetime.today() os.chdir(path) #creating a log file with date file=open(logging_path+datetime.datetime.today().strftime('%d-%m-%Y')+'.txt','a') for root,directories,files in os.walk(path,topdown=False): for name in files: t = os.stat(os.path.join(root, name)) filetime = datetime.datetime.fromtimestamp(t) - today if filetime.days <= -7: print(os.path.join(root, name), filetime.days) file.write(os.path.join(root, name)+' created '+str(-1*filetime.days)+' days ago\n') #writing in the file os.remove(os.path.join(root, name))
First, we need to create a log directory. If that directory is available then we print Directory is already available. then we create a text file with today’s date as a name in append mode. Then we write in that file File full path and name and how many days before that file was created. That is enough about logging. By using this simple technique, you can check earlier deleted files by the same script.
Making your file deletion python script dynamic
Yes, we automated the deleted process but it kind of boring if you have to change path in the script every time. So, what we are going to do now is you can run files from outside and add a folder path in argument and our code will take care of all other things. Let’s do that now.
import os import datetime import glob import sys path = sys.argv #argv is your current python file and argv is first argument where you want to delete files from logging_path='Enter your log directory path' if not os.path.isdir(logging_path): os.mkdir(logging_path) else: print("Directory already exists") today = datetime.datetime.today() os.chdir(path) file=open(logging_path+datetime.datetime.today().strftime('%d-%m-%Y')+'.txt','a') for root,directories,files in os.walk(path,topdown=False): for name in files: t = os.stat(os.path.join(root, name)) filetime = datetime.datetime.fromtimestamp(t) - today if filetime.days <= -7: print(os.path.join(root, name), filetime.days) file.write(os.path.join(root, name)+' created '+str(-1*filetime.days)+' days ago\n') os.remove(os.path.join(root, name))
Here, we first imported the sys package. sys package is a useful package for managing command-line arguments. Here, the filename is the first argument and our path is the second argument. Thus we set argv as our path. After that, our whole code is the same. Remember, don’t use that code unnecessarily. Otherwise, it will delete your files.
That’s an easy way to delete files. Let me show you my outputs.
Wooh…we are done with the coding part. But you have the correct question in mind. Do we need to run this script manually? The answer is no. We can schedule this task using a task scheduler in windows.
Automate Code Using Task Scheduler on Windows
First, you need to open the task scheduler. Then on the right side, you can see create basic task options. After clicking on that new window will open.
No, you need to enter name and description. Then go to the next screen
After this, you have to give a starting date and time. Then on the next screen choose to start a program. On the final screen choose program path and argument as to which folder you want to clean.
After following every step perfectly, your script will run automatically and you have absolutely automate whole boring stuff using python.
Subscribe to our newsletter
Error SendFox Connection:
Sorry, too many requests.