user1681573
user1681573

Reputation: 933

Checking a list to make a decision

all.

I'm trying to write a script that checks the age of some files in a folder, and deletes the old files if new files have been created. If new files were not created, then it does not delete the old files and it sends me an email telling me that no new files were created.

I use the OS module ctime to check the age of files, and I'm using an external script "sendmail" to handle the emailing.

As it works now, it correctly determines old and new files, and then deletes old files, but it does not correctly make the decision about whether or not to call sendmail. Let me show you:

for fn in os.listdir(path, f)
 fn = os.path.jion(path, f)
 ctime = os.stat(fn).st_ctime
 if ctime > now - 1 * 86400: #this is a new file
  new_files.append(fn)
  countit = new_files.count(fn) #counting the occurence of appended files
 if new_Files.count(fn) > countit: #checks the list
  import sendmail
   sendmail
 elif ctime < now - 10 * 86400: #checking for old file
  old_files.append(fn)
if new_files:
 for fn in old_files:
  os.remove(fn)

So, can I get some help on this? I'm really stuck. Should I be using an elif statement to check my list, like so:

if ctime > now - 1 * 86400:
 new_files.append(fn)
elif:
 import sendmail
  sendmail

Is that a proper way to write that? Is there a correct way to write this decision? Is my whole script wrong?

EDIT - Sorry if I'm being whiny, I've been working on this for some time, and it's very frustrating. I appreciate whatever help you can give!!!

Upvotes: 0

Views: 100

Answers (3)

Matt
Matt

Reputation: 3741

It looks like you probably want to do something like this:

for fn in os.listdir(path, f):
    fn = os.path.join(path, f)
    ctime = os.stat(fn).st_ctime
    if ctime > now - 1 * 86400: #this is a new file
        new_files.append(fn)
        countit = new_files.count(fn) #counting the occurrence of appended files
    elif ctime < now - 10 * 86400: #checking for old file
        old_files.append(fn)
if new_files:
    for fn in old_files:
        os.remove(fn)
else:
    import sendmail
        sendmail.sendmail()

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799430

I use the OS module ctime to check the age of files...

Whoops! ctime isn't when the file was created on all operating systems; do not rely on it unless you are certain which OS you are on. mtime is the last modification time instead, but is supported on all operating systems.

Upvotes: 0

HardcoreBro
HardcoreBro

Reputation: 425

I'm having trouble determining what exactly sendmail is doing/what its supposed to do. Could you post that module as well?

Also, I would recommend putting the import statement at the top of the file, so that you don't have to import the file multiple times. This should speed up performance significantly.

Also, I'm having trouble understanding what the line "os.listdir(path, f)" is doing. Whenever I try the method os.listdir with more than 1 argument, I get an error. Is this a typo?

Upvotes: 0

Related Questions