Christian Schou
  • Casa
  • Blog
    • Programação
      • C#
      • PowerShell
      • Python
      • SQL
    • WordPress
      • Tutoriais
    • Nuvem
    • Automação residencial
      • Assistente Doméstico
        • Node-Red
    • Career
  • Serviços
  • Glossário
  • About
No Result
View All Result
Christian Schou
  • Casa
  • Blog
    • Programação
      • C#
      • PowerShell
      • Python
      • SQL
    • WordPress
      • Tutoriais
    • Nuvem
    • Automação residencial
      • Assistente Doméstico
        • Node-Red
    • Career
  • Serviços
  • Glossário
  • About
No Result
View All Result
Christian Schou
No Result
View All Result
Home Programação Python
delete files and folders

How to delete files and folders in a directory older than X days

by cristã
sábado, outubro 9, 2021
in Python
0

If you are generating files or folders programmatically every day, hour, a minute, or second… Then you will end up with a lot of files that potentially may take up a lot of space on your system. Space is not a thing we got unlimited of and due to that, it is often necessary to delete files within a directory that are older than a particular number of days.

I have often implemented this functionality in archive directories where the server should delete files older than 3 months due to GDPR compliance. Without performing automatic routine maintenance, these archive folders can get big and start to consume a lot of space on our servers or cloud storage. Instead of doing this manually (chance you will forget it big) we can create a very simple script that takes a few parameters and allows us to remove files to clean up and maintain the server to keep it healthy.

Tabela de conteúdos
  1. Create simple script to delete files older than X days at hardcoded path
  2. Create a script that takes two arguments to delete files older than X days at X path
  3. Summary

Create simple script to delete files older than X days at hardcoded path

The first thing we have to do is creating a python script (I’m using Visual Studio Code with the Python addon). Start by adding the shebang at line one (A shebang is a path to Python executables) and then import the following packages to perform the activities.

#!/usr/bin/python3
                                            
import os, time, shutil

Now we have to define how old the files or folders within the directory are allowed to be. This can be done by adding two variables, one for the time and one for the root path (the directory that we would like to maintain). Python is working with seconds when we are talking time, so in order for us to be able to make it into days, we would have to multiply the number of days by 86400 (this is the number of seconds for one day). This calculated value will then at runtime be subtracted from the current timestamp giving us the date for how old we allow the files to be. Also, remember to declare the path to the root directory. If you are on Windows, you can add “r” in front of the string in order to be able to use a double backslash (\).

Thirty_days_ago = time.time() - (30 * 86400)
root = "/path/to/directory"

Now for the fun part – where we actually make the cleanup process for files and folders. The rest of the script will be relying mostly on the OS module that we imported in the beginning. This module will be responsible for listing contents within the directory, help us determine if the object is a file or directory, and delete files. The module called shutil is used for handling directories and in this script, we will use it to delete a directory. The script will be using a for loop to loop over each object within the root directory. Below is the code and I will be explaining what the different parts of the code are doing:

for i in os.listdir(root):
    path = os.path.join(root, i)

    if os.stat(path).st_mtime <= thirty_days_ago:

        if os.path.isfile(path):
            try:
                os.remove(path)

            except:
                print("Could not remove file:", i)

        else:
            try:
                shutil.rmtree(path)
	    
            except:
                print("Could not remove directory:", i)

As you can see above the script is looping over each object listed in the root directory. It’s then comparing the creation time of the object to the timestamp we calculated in the beginning and then removing it if the if statement is evaluated to true.

Below is the complete script you can copy and use in your own environment. If you would like to be able to use this as a dynamic script, please check my solution below, where you can append arguments at execution time for time and path.

#!/usr/bin/python3

import os, time, shutil

# Declare how old files are allowed to be and set the root directory
thirty_days_ago = time.time() - (30 * 86400)
root = "/home/king/nextcloud_server_backups"

# List all files and folders in the root directory
for i in os.listdir(root):
    # Concatenate the root path with the file or folder we got from the above listing method
    path = os.path.join(root, i)

    # Check if the file or folder is older than 30 days
    if os.stat(path).st_mtime <= thirty_days_ago:

	# If that is true and the object is a file, try to remove it
        if os.path.isfile(path):
            try:
                os.remove(path)

	    # File could not be moved, show an error
            except:
                print("Could not remove file:", i)

	# If the object was not a file, then it must be a folder
        else:
	    # Try to remove the folder from the directory
            try:
                shutil.rmtree(path)
	    
	    # The folder could not be removed, show an error
            except:
                print("Could not remove directory:", i)

Create a script that takes two arguments to delete files older than X days at X path

If you like me often want this to be running automatically carried out by a service calling this script, you would want to make use of system arguments. It’s pretty easy and straightforward to implement this nice feature.

Start by adding sys to the import at line 3, as shown below:

Then remove the “old” time and path declarations and replace them with the following two declarations:

docPath = sys.argv[1]
days = sys.argv[2]

The two above declarations take one argument each. The first one allows you from the terminal to specify the path and the second one allows for specifying the number of days we allow the files to be within the directory.

Now add the following line. It takes the declaration from days and casts it into an integer in order for us to do some math and calculate the timestamp later.

days_ago = time.time() - (int(days) * 86400)

The only thing I changed in the main part of the script was to match the new names of my declarations.

for i in os.listdir(docPath):
    path = os.path.join(docPath, i)

    if os.stat(path).st_mtime <= days_ago:
        if os.path.isfile(path):
            try:
                os.remove(path)

            except:
                print("Could not remove file: ", i)
        
        else:
            try:
                os-rmtree(path)

            except:+
                print("Could not remove directory: ", i)

Below is the full script to copy and implement into your setup:

import os, time, shutil, sys

docPath = sys.argv[1]
days = sys.argv[2]

days_ago = time.time() - (int(days) * 86400)
#docPath = r"C:\Users\chs\Desktop\removeTest"

for i in os.listdir(docPath):
    path = os.path.join(docPath, i)

    if os.stat(path).st_mtime <= days_ago:
        if os.path.isfile(path):
            try:
                os.remove(path)

            except:
                print("Could not remove file: ", i)
        
        else:
            try:
                os-rmtree(path)

            except:+
                print("Could not remove directory: ", i)

Summary

In this tutorial, you have learned how to use the os module to list all contents of a directory and shutil to remove the files that are too old to stay in our directory. You have also seen how to use arguments and parse them to the script from the terminal. If you got any issues or suggestions, please let me know in the comments and I will try to help you. As always – Happy Coding!

Tags: argumentscleanupFilesmaintenanceosroutineshutilsys
Previous Post

How to use Dapper with ASP.NET Core and Repository Pattern

Next Post

How to get started with Node-RED and Home Assistant

cristã

cristã

Hello 👋 My name is Christian and I am 26 years old. I'm an educated Software Developer with a primary focus on C#, .NET Core, Python, and PowerShell. Currently, I'm expanding my skills in Software Robots and Cloud Architecture. In some of my spare time, I share my knowledge about tech stuff on my blog.

Related Posts

scrape prices, web scrape
Python

How to scrape prices from competitors e-commerce websites using Python

by cristã
sexta-feira, janeiro 7, 2022
6

Running an e-commerce business in today's world can be quite a job. Everyone is trying to beat each other with...

Read more
stock price scraper

How to make a STOCK price scraper command-line application using Python and Web Scraping

segunda-feira, dezembro 6, 2021
Next Post
node-red

How to get started with Node-RED and Home Assistant

Christian Schou

Christian Schou

Software Developer

Hello - my name is Christian and I am 26 years old. I'm an educated Software Developer with a primary focus on C#, .NET Core, Python, and PowerShell. Currently, I'm expanding my skills in Software Robots and Cloud Architecture. In some of my spare time, I share my knowledge about tech stuff on my blog.

Recent articles

personal website
Career

Top 6 things to add on your personal website to get hired for a tech job

by cristã
domingo, agosto 7, 2022
0

Back in the days before the internet was a thing like it is today, we used to have business cards...

Read more
watchdog

The #1 guide to show real-time .NET 6 logs for Web Apps and APIs in a modern way using WatchDog for Free

sábado, agosto 13, 2022
get hired for a tech job

5 tips to help you get hired for a tech job

domingo, julho 31, 2022
restful web api

How to build a RESTful Web API using ASP.NET Core and Entity Framework Core (.NET 6)

segunda-feira, julho 25, 2022
dynamically register entities

How to Dynamically Register Entities in DbContext by Extending ModelBuilder?

sábado, julho 23, 2022

Christian Schou

Software Developer

Hello - my name is Christian and I am 26 years old. I'm an educated Software Developer with a primary focus on C#, .NET Core, Python, and PowerShell. Currently, I'm expanding my skills in Software Robots and Cloud Architecture. In some of my spare time, I share my knowledge about tech stuff on my blog.

Recent articles

personal website

Top 6 things to add on your personal website to get hired for a tech job

domingo, agosto 7, 2022
watchdog

The #1 guide to show real-time .NET 6 logs for Web Apps and APIs in a modern way using WatchDog for Free

sábado, agosto 13, 2022
get hired for a tech job

5 tips to help you get hired for a tech job

domingo, julho 31, 2022
  • pt_BRPortuguês do Brasil
    • da_DKDansk
    • en_USEnglish
    • de_DEDeutsch
    • hi_INहिन्दी
  • Contact
  • Política de privacidade
  • Termos de serviço

© 2022 Christian Schou - All rights reserved.

No Result
View All Result
  • Casa
  • Blog
    • Programação
      • C#
      • PowerShell
      • Python
      • SQL
    • WordPress
      • Tutoriais
    • Nuvem
    • Automação residencial
      • Assistente Doméstico
    • Career
  • Serviços
  • Glossário
  • About

© 2022 Christian Schou - All rights reserved.

Eu uso cookies no meu site para lhe dar a experiência mais relevante, lembrando suas preferências e visitas repetidas. Ao clicar em “Aceitar”, você concorda com o uso de TODOS os cookies.
Não vender minhas informações pessoais.
Configurações de cookiesACCEPT
Política de Privacidade e Cookies

Visão geral da privacidade

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Sempre ativado
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
CookieDuraçãoDescrição
__gads1 year 24 daysThe __gads cookie, set by Google, is stored under DoubleClick domain and tracks the number of times users see an advert, measures the success of the campaign and calculates its revenue. This cookie can only be read from the domain they are set on and will not track any data while browsing through other sites.
_ga2 yearsThe _ga cookie, installed by Google Analytics, calculates visitor, session and campaign data and also keeps track of site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognize unique visitors.
_ga_0J2F6JVWSD2 yearsThis cookie is installed by Google Analytics.
_gat_gtag_UA_84232734_11 minuteSet by Google to distinguish users.
_gid1 dayInstalled by Google Analytics, _gid cookie stores information on how visitors use a website, while also creating an analytics report of the website's performance. Some of the data that are collected include the number of visitors, their source, and the pages they visit anonymously.
YouTube2 yearsYouTube sets this cookie via embedded youtube-videos and registers anonymous statistical data. I embed YouTube videos in my articles/tutorials - you won't get the full experience of the articles if this is deactivated.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
CookieDuraçãoDescrição
IDE1 year 24 daysGoogle DoubleClick IDE cookies are used to store information about how the user uses the website to present them with relevant ads and according to the user profile.
test_cookie15 minutesThe test_cookie is set by doubleclick.net and is used to determine if the user's browser supports cookies.
VISITOR_INFO1_LIVE5 months 27 daysA cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface.
YSCsessionYSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages.
yt-remote-connected-devicesneverYouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
yt-remote-device-idneverYouTube sets this cookie to store the video preferences of the user using embedded YouTube video.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SALVAR E ACEITAR
Desenvolvido por CookieYes Logo