Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    28
    Rep Power
    0

    Delete Files after a certain date


    Does anyone know of a module, function or any other way of solving my problem using python:

    I need to come up with something that searches a list of folders for files created on or before a certain date, and deletes them. I can't figure out how to read a files attributes for the date created/modified. I am a virgin to Python, and would be executing this script on OS X. Any thoughts would help out Greatly.

    Thanks in Advance.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    you should look into the os module for python. it comes with python and it will help you do everything you are asking. It will also cross platform so it doesn't jsut ahve to run on Mac OSX
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    Here would be a script:

    Code:
    import os,time
    
    date = "23"
    
    if date in asctime():
        os.remove(...)
    Tell me if it is not what you want
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    The os.stat function returns an object with the information you need (plus lots of other stuff).

    on Windows, os.stat(filename).st_ctime will return the creation time in seconds.

    The code you want is something like this:

    Code:
    # WARNING: untested code - use at your own risk.
    
    import os
    
    for (path, dirs, files) in os.walk(start):
       for file in files:
         filename = os.path.join(path, file)
         if os.stat(filename).st_ctime  < REMOVE_TIME:
            os.remove(filename)
    You can use functions in the time module to convert the REMOVE_TIME into the correct format.

    Dave - The Developers' Coach

    Comments on this post

    • Yegg` agrees
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    28
    Rep Power
    0
    thanks a lot for the quick replies. I will let you guys know how it works out and if I have any other questions. Thanks again for all the help.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    28
    Rep Power
    0
    How does this look so far? constructive criticism appreciated.

    Code:
    import os,EasyDialogs
    
    version = 1.01
    directory =  EasyDialogs.Askfolder()
    
    EasyDialogs.Message("Welcome to the EPS Wiper v", version[, ok=none])
    EasyDialogs.AskFolder(["Please Locate the EPS Folder: "][, Desktop][, "Please Locate the EPS Drive"][, string])
    
    
    for (directory) in os.walk(start):
       for file in files:
         filename = os.path.join(path, file)
         if os.stat(filename).st_ctime  < REMOVE_TIME:
            os.remove(filename)
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    could you maybe give us the EasyDialog Module because I dont have it.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    28
    Rep Power
    0
    http://www.averdevelopment.com/python/

    is the only place I have found it.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    The first part why do you define a new version(I get an error) and ask folder why does it have lists everywhere
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    28
    Rep Power
    0
    So .. I have canned the new version variable, and switched some things around;
    Code:
    import os,EasyDialogs
    
    EasyDialogs.Message("Welcome to the EPS Wiper v1.01")
    directory = EasyDialogs.AskFolder("Please Locate the EPS Folder: ")
    
    for root, dirs, files in os.walk(directory):
       for file in files:
         filename = os.path.join(directory, file)
         if os.stat(filename).st_ctime  < REMOVE_TIME:
            os.remove(filename)
    
    EasyDialogs.Message("I'm all done now. See you next time.\n -the EPS Wiper")
    The thing I keep getting stuck on now is the REMOVE_TIME. I think that this is supposed to be the time to start deleting before but I don't know what sytax to use to type it here (seconds, minutes, days, etc). Do I simply type something like "777600" for nine days worth of seconds?
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    28
    Rep Power
    0
    I would like to apologize for asking a question that was already answered in the above thread. I have since figured out the time problem.

    I have encountered a new problem that is stumping me: When I run the script it works fine as long as it has something in the root but otherwise I get this error:

    Traceback (most recent call last):
    File "image_wiper.py", line 32, in ?
    if os.stat(filename).st_ctime < time.ctime(604800):
    OSError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\Februus\\Desktop\\IMAGES\\e ball python male 100 11-02_jpg.jpg'
    I have my folders structured as so:

    \IMAGES
    ....\100
    .......*.jpg
    ....\200
    .......*.jpg
    ....\300
    .......*.jpg
    ....\400
    .......*.jpg

    and all of the images are contained inside the numbered folders, not the "IMAGES" root . What is it that is making python try to find the image in the root and not the numbered folder?
  22. #12
  23. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You have to post some code if you want us to be able to debug it, that might seem a little obvious . Anyway this code should work for you in the mean time:

    Code:
    #!/usr/bin/env python
    
    import os, glob, time
    
    root = 'images/'
    date = '2005-04-27' #The expiry date for the images in the format YYYY-MM-DD.
    
    for folder in glob.glob(root + '[1-9]00*'):
        #Loops over each subfolder in the 'root' directory that match the pattern
        #(a number between 1 and 9 followed by two or more Zeros).
        for image in glob.glob(folder + '/*.jpg'):
            #We then loop over each JPEG in turn and decide whether it should be
            #deleted.
    
            stats = os.stat(image) #Retrieves the stats() for the current image.
            if time.localtime(stats[8]) < time.strptime(date, '%Y-%m-%d'):
                #Checks if the images last modified date is less than the 'date'
                #provided before attempting to remove it.
                try:
                    print 'Removing', image
                    os.remove(image)
                except OSError:
                    #If an error occurs while trying to remove the image print
                    #a warning message.
                    print 'Couldnt remove', image
    Note: I haven't tested this too much. The comments pretty much explain what's going on .

    Mark.
    Last edited by netytan; April 26th, 2005 at 03:44 PM.
    programming language development: www.netytan.com Hula

  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    28
    Rep Power
    0
    Here is my code so far:

    Code:
    import os,EasyDialogs,time
    
    EasyDialogs.Message("Welcome to the Image Wiper v1.01")
    directory = EasyDialogs.AskFolder("Please Locate the Image Folder: ")
    
    for root, dirs, files in os.walk(directory):
       for file in files:
         filename = os.path.join(directory, file)
         if os.stat(filename).st_ctime < time.ctime(6):
            os.remove(filename)
         
    EasyDialogs.Message("I'm all done now. See you next time.\n -the Image Wiper")
  26. #14
  27. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    As you're error says it can't find the file you want to remove - normally I would put this in a try block to catch any errors if for some reason the file can't be removed like in the code above. This is because your the path your using to point to your file is wrong .

    You should be able to solve the problem easily by joining root and file instead of directory and file. In general it's not a good idea to use the name of a built-in function as a variable name in your program, in this case file().

    You might also want to consider using Applescript to do this. It would probably be more effective too since you can set up a Folder Action to run the script under certain conditions .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    28
    Rep Power
    0
    Originally Posted by netytan
    As you're error says it can't find the file you want to remove - normally I would put this in a try block to catch any errors if for some reason the file can't be removed like in the code above. This is because your the path your using to point to your file is wrong .

    You should be able to solve the problem easily by joining root and file instead of directory and file. In general it's not a good idea to use the name of a built-in function as a variable name in your program, in this case file().

    You might also want to consider using Applescript to do this. It would probably be more effective too since you can set up a Folder Action to run the script under certain conditions .

    Hope this helps,

    Mark.
    Thanks Mark! The root thing makes it work. There is one underlying problem now. I can't get it to delete files before the date given. If I switch greater than "<" to less than ">" it doesn't do a thing. I mean the EasyDialogs work fine, and everything seems to execute properly, but when you visit the folder to check for deleted files, nothing has happend.
    Code:
    import os,EasyDialogs,time
    
    EasyDialogs.Message("Welcome to the Image Wiper.\n\n\t\t\t\t v1.01")
    directory = EasyDialogs.AskFolder("Please Locate the Image Folder: ")
    date = EasyDialogs.AskString("On what date would you like to begin deleting from?\n\t format = YYYY-MM-DD", "2005-4-28")
    
    for root, dirs, files in os.walk(directory):
    	for file in files:
         		filename = os.path.join(root, file)	
    		if os.stat(filename).st_ctime > time.strptime(date, '%Y-%m-%d'):
           			os.remove(filename)
         
    EasyDialogs.Message("I'm all done for now. See you next time.\n-the Image Wiper")
    Is "os.stat(filename).st_ctime" reporting time in a different format than "time.strptime(date, '%Y-%m-%d')"? I am stumped yet again.

    Thanks for all the help.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo