[Tutor] deleting files that are older than x days old

Bobby Castleberry bob at farms.coop
Wed Jul 13 19:55:53 CEST 2005


Mike Hansen wrote:

> From a python program, I want to delete certain files that are older than x 
>days old.
>
>How do you convert a time represented in seconds from the epoch into a datetime 
>object?
>  
>
using unix time (seconds from the epoch) is rather easy.  what I would
do in this case is something along these lines:
import time
import os.path
import os
path = ("/home/joe/test.sxc")
ftime = os.path.getmtime(path)
curtime = time.time()
difftime = curtime - ftime
if difftime > 604800:
  os.unlink(path)


-------------------
coming from the linux world and most of my programming being in bash i'm
very comfortable working in seconds (it's nice not to worry about all
the conversion and just deal with actual math), 604800 is a week fyi,
3600 seconds in an hour, and 86400 seconds in a day. of course that
doesn't answer your question, to answer your question look at ctime in
the time module or I believe strftime would also work

Bob
new to python and still trying to grasp oo programming





More information about the Tutor mailing list