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?

Recommended Answers

All 6 Replies

Use the wait() method on a Threading.Event object or a Threading.Condition object, with a timeout.

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?

you can sort the times and wait for each time in turn.

Solved?

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.

;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.