Python程序崩溃

时间:2012-12-14 21:11:26

标签: python permissions crash

所以我设计了一个在计算机上运行的程序,查找困扰我们的文件的特定方面,并在传递标志时删除文件。不幸的是,程序似乎几乎随机关闭/崩溃。我几乎随机地说,因为程序在删除文件后总是会退出,尽管它会在成功后保持不变。

我运行了一个并行的Python程序,它在相同的时间间隔内向上计数,但不执行任何其他操作。该程序不会崩溃/退出,并保持打开状态。

是否存在R / W访问问题?我以管理员身份运行该程序,所以我不确定为什么会出现这种情况。

以下是代码:

import glob
import os
import time
import stat

#logging
import logging
logging.basicConfig(filename='disabledBots.log')
import datetime


runTimes = 0
currentPhp = 0
output = 0
output2 = 0
while runTimes >= 0:
    #Cycles through .php files
    openedProg = glob.glob('*.php')
    openedProg = openedProg[currentPhp:currentPhp+1]
    progInput = ''.join(openedProg)
    if progInput != '':
        theBot = open(progInput,'r')

        #Singles out "$output" on this particular line and closes the process
        readLines = theBot.readlines()
        wholeLine = (readLines[-4])
        output = wholeLine[4:11]

        #Singles out "set_time_limit(0)"
        wholeLine2 = (readLines[0])
        output2 = wholeLine2[6:23]

        theBot.close()
    if progInput == '':
        currentPhp = -1

    #Kills the program if it matches the code
    currentTime = datetime.datetime.now()
    if output == '$output':
        os.chmod(progInput, stat.S_IWRITE)
        os.remove(progInput)
        logging.warning(str(currentTime) +' ' + progInput + ' has been deleted. Please search for a faux httpd.exe process and kill it.')
        currentPhp = 0

    if output2 == 'set_time_limit(0)':
        os.chmod(progInput, stat.S_IWRITE)
        os.remove(progInput)
        logging.warning(str(currentTime) +' ' + progInput + ' has been deleted. Please search for a faux httpd.exe process and kill it.')
        currentPhp = 0
    else:
        currentPhp = currentPhp + 1
        time.sleep(30)

    #Prints the number of cycles    
    runTimes = runTimes + 1
    logging.warning((str(currentTime) + ' botKiller2.0 has scanned '+ str(runTimes) + ' times.'))
    print('botKiller3.0 has scanned ' + str(runTimes) + ' times.')

2 个答案:

答案 0 :(得分:1)

首先,如果你的代码基于这样的代码,那么找出正在发生的事情将会变得更加容易......

for fname in glob.glob('*.php'):
    with open(fname) as fin:
        lines = fin.readlines()
    if '$output' in lines[-4] or 'set_time_limit(0)' in lines[0]:
        try:
            os.remove(fname)
        except IOError as e:
            print "Couldn't remove:", fname

而且,错误的是,目前实际上并不是第二次,你的现有代码太难以跟上fullstop了,更不用说可能导致我们还不知道的奇怪错误的所有内容!

答案 1 :(得分:0)

if os.path.exists(progInput): 
    os.chmod(progInput, stat.S_IWRITE)
    os.remove(progInput)

ALSO:

你永远不会重置循环中的output或output2变量吗?

这是故意的吗?