Hi How can I delete a file after a certain time?

for example if I want to delete a file after 08.30 has passed?

do I use import OS? and import time?

Maybe this could help you to catch some file operation and using time.sleep:

import os
import time
dirname = 'test'
try:
    os.mkdir(dirname)
except:
    pass

fn = os.path.join(dirname, 'test.txt')

with open(fn, 'w') as f: f.write('Nothing')
print 'Created %s (%s)' % (fn, time.asctime())

print os.listdir(dirname)
time.sleep(8.30)
os.remove(fn)

print os.listdir(dirname)
print 'Deleted %s (%s)' % (fn, time.asctime())

It must be in a function. I have 4 files that needs to be deleted within 4 different times. How can I do that then? Can I make it like:

def remove(filename,time)
                if time os.remove(filename)

I have dont know?

Edited 6 Years Ago by ekkanh: n/a

What about if the python script not running???

I think you need a cron job. With cron job you are in win win situation for this operation.

;)

Edited 6 Years Ago by richieking: n/a

This question has already been answered. Start a new discussion instead.