Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6821525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:33:19+00:00 2026-05-26T21:33:19+00:00

I have a script for deleting images older than a date. Can I pass

  • 0

I have a script for deleting images older than a date.

Can I pass this date as an argument when I call to run the script?

Example: This script delete_images.py deletes images older than a date (YYYY-MM-DD)

python delete_images.py 2010-12-31

Script (works with a fixed date (xDate variable))

import os, glob, time

root = '/home/master/files/' # one specific folder
#root = 'D:\\Vacation\\*'          # or all the subfolders too
# expiration date in the format YYYY-MM-DD

### I have to pass the date from the script ###
xDate = '2010-12-31' 

print '-'*50
for folder in glob.glob(root):
    print folder
    # here .jpg image files, but could be .txt files or whatever
    for image in glob.glob(folder + '/*.jpg'):
        # retrieves the stats for the current jpeg image file
        # the tuple element at index 8 is the last-modified-date
        stats = os.stat(image)
        # put the two dates into matching format    
        lastmodDate = time.localtime(stats[8])
        expDate = time.strptime(xDate, '%Y-%m-%d')
        print image, time.strftime("%m/%d/%y", lastmodDate)
        # check if image-last-modified-date is outdated
        if  expDate > lastmodDate:
            try:
                print 'Removing', image, time.strftime("(older than %m/%d/%y)", expDate)
                os.remove(image)  # commented out for testing
            except OSError:
                print 'Could not remove', image 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:33:20+00:00Added an answer on May 26, 2026 at 9:33 pm

    The quick but crude way is to use sys.argv.

    import sys
    xDate = sys.argv[1]
    

    A more robust, extendable way is to use the argparse module:

    import argparse
    
    parser=argparse.ArgumentParser()
    parser.add_argument('xDate')
    args=parser.parse_args()
    

    Then to access the user-supplied value you’d use args.xDate instead of xDate.

    Using the argparse module you automatically get a help message for free when a user types

    delete_images.py -h
    

    It also gives a helpful error message if the user fails to supply the proper inputs.

    You can also easily set up a default value for xDate, convert xDate into a datetime.date object, and, as they say on TV, “much, much more!”.


    I see later in you script you use

    expDate = time.strptime(xDate, '%Y-%m-%d')
    

    to convert the xDate string into a time tuple. You could do this with argparse so args.xDate is automatically a time tuple. For example,

    import argparse
    import time
    
    def mkdate(datestr):
        return time.strptime(datestr, '%Y-%m-%d')
    parser=argparse.ArgumentParser()
    parser.add_argument('xDate',type=mkdate)
    args=parser.parse_args()
    print(args.xDate)
    

    when run like this:

    % test.py 2000-1-1
    

    yields

    time.struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=1, tm_isdst=-1)
    

    PS. Whatever method you choose to use (sys.argv or argparse), it would be a good idea to pull

    expDate = time.strptime(xDate, '%Y-%m-%d')
    

    outside of the for-loop. Since the value of xDate never changes, you only need to compute expDate once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have wrote this script and I was wounder if I can have it
I have script like this : fullscript.sh if [ ! -f /opt/laks/linux-2.6.33.2.tar.bz2 ] then
I have a script that parses the filenames of TV episodes (show.name.s01e02.avi for example),
I have a script which contains two classes. (I'm obviously deleting a lot of
please help me with 1 more PL/pgSQL question. I have a PHP-script run as
i have this script found here http://jsfiddle.net/g4txt/ i am using this carousel http://www.thomaslanciaux.pro/jquery/jquery_carousel.htm the
I have made a script that run various loops and does some SQL inserts.
I have this code: <meta http-equiv=content-type content=text/html; charset=<?php echo $stigConfig['charset']; ?> /> How can
anyone have script or procedures to install SQL Server 2008 Express, set up the
I have script file where a command is stored in a variable First i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.