0

I have a script that counts the number of files in a folder, and if it's less than a max number, it creates new files until there is the correct number. Ideally, as old files are deleted, the new ones should be generated. The problem is, my code sometimes wont create a new file if only one file is deleted at a time. Sometimes it will, sometimes it wont. It will always create new files if more than one is deleted at a time.

import os

cfcount = 0
maxcalls = 7
run = 1
filecount = 0

def callFile(channel, maxretries, retrytime, waittime, context, ext):
    #create sting
    return callfile

def getCount():
    """Gets the number of callfiles in the directory"""
    count = 0
    files = os.listdir("c:\\proc")
    for file in files:
            if os.path.isfile("c:\\proc\\" + file):
                count += 1
                #print (count)
    return count

run = 1
while run == 1:
    """main loop that runs until there's no more people left to call"""
        filecount = getCount()    
        print (filecount)
        lacking_filecount = maxcalls - filecount
    while lacking_filecount > 0:
        cfcount += 1
        f = open("c:\proc\callfile" + str(cfcount) + ".call", 'w')
        f.write(callFile("SIP/200", '0', '0', '45', "call-file-test", '200'))
        f.close()
        print ("Filecount: " + str(filecount))
        print ("Callfile number: " + str(cfcount))
        lacking_filecount -= 1

I've been able to get it to work every time without fail IF I keep the print(filecount) statement. If I remove that statement, it works sometimes and sometimes it doesn't.

This is the output

>>> 
0
Filecount: 0
Callfile number: 1
Filecount: 0
Callfile number: 2
Filecount: 0
Callfile number: 3
Filecount: 0
Callfile number: 4
Filecount: 0
Callfile number: 5
Filecount: 0
Callfile number: 6
Filecount: 0
Callfile number: 7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7

without the print(filecount) it looks like this. It worked until about number 9 then it quit replacing new files if only one was deleted. Notice it still prints a callfile number.

>>> 
Callfile number: 1
Callfile number: 2
Callfile number: 3
Callfile number: 4
Callfile number: 5
Callfile number: 6
Callfile number: 7
Callfile number: 8
Callfile number: 9
Callfile number: 10
Callfile number: 11
Callfile number: 12

This is output from print(file) inside the getCount() loop. It hasn't not worked yet with that print statement added.

Callfile number: 17
callfile1.call
callfile17.call
callfile2.call
callfile3.call
callfile4.call
callfile5.call
callfile6.call
callfile1.call
callfile17.call
5
  • 3
    No need for while run == 1, Python has bool's, this ain't C :) Commented Mar 17, 2011 at 4:42
  • On further inspection, I think I misinterpreted the problem. It seems as if there's more to the loop than you've posted -- where is it terminated? If it runs in an infinite loop, why is it a problem that a deleted file isn't detected if it will just be detected the next iteration? Unless you're saying getCount() consistently returns the wrong output (sometimes) when a single file is deleted (in which case, the platform you're using may be relevant)? Commented Mar 17, 2011 at 4:55
  • I'm not sure why but it does get detected in the next loop and it inciments cfcount but it doesn't create the file IF only one has been deleted and only sometimes. Some other times it works just fine. Ive tried starting with empty folders and full folders and it just seems random to me. I'm very new to linux but i feel like I should be looking for a way to hook into an event that gets fired when a file is deleted. Commented Mar 17, 2011 at 5:14
  • Post that part (filesystem events) as a separate question. Is the output of os.listdir() always what you expect it to be? Commented Mar 17, 2011 at 5:25
  • Alright, I'm stumped, sorry. Post an answer when you figure it out, I'm curious! Commented Mar 17, 2011 at 22:52

1 Answer 1

2

Seems like a race condition between the file being created and the file showing up in os.listdir() output. The print statement probably takes just enough time to "fix" this on your system, most of the time.

Why not change your logic so that it only calls getCount() once, then creates the appropriate amount of files?

filecount = getCount()
lacking_filecount = maxcalls - filecount
while lacking_filecount > 0:
    # Create file...
    lacking_filecount -= 1

Also, you seem to have an infinite loop since you never set run inside the loop.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you so much. I was getting frustrated. that's what i should have done in the first place. the infinite loop is just because i haven't figured how many times to run it yet.
@rhys: Great, on afterthought I wasn't sure what the problem was :-) Note that if you want a loop that runs until some terminating condition that (for whatever reason) you don't want to put in the condition part of the loop statement, the usual way to do it is with while True: and then a break statement somewhere in the loop. It's a little cleaner and clearer than with a control variable
I updated the code and it actually does the same thing. its still faster though. I'm going to look at what you said since.
maybe it's because it's on windows? ultimately i'm running it on linux but I didn't think it would matter.
@rhys: When a single file is deleted, are you still seeing it in the output from os.listdir() (sometimes)? (I'm still trying to get the problem straight)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.