开发者

Python loop problems

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 do开发者_C百科esn'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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜