2012-12-14 115 views
0

因此,我設計了一個程序,該程序在計算機上運行,​​查找困擾我們的文件的特定方面,並在標誌通過時刪除文件。不幸的是,該程序似乎幾乎是隨機關閉/崩潰。我幾乎隨機地說,因爲程序在刪除文件後總是退出,但它通常會在成功後保持不變。Python程序崩潰

我已經運行了一個並行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.') 
+0

當它失敗時,它是否在os.remove之後的日誌中記錄消息? – Mark

+5

Python程序通常會在stderr上產生異常追溯,如果它們崩潰的話。你看到什麼樣的錯誤? – larsks

+0

尚未設置錯誤消息日誌記錄。什麼是添加它的好方法? – user1698693

回答

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

ALSO:

你永遠不會重置環路輸出或輸出2變量?

這是故意的嗎?

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 

而犯錯,這實際上不是其次在一時之間,您現有的代碼太棘手,無法完全遵循,更不用說所有可能導致我們還不知道的奇怪錯誤的位了!

+0

對不起,代碼很詭異。我(顯然)是這個東西的新手,所以我只是使用我理解的構建塊。我會嘗試調整代碼以適應您在此處的格式。 謝謝。 – user1698693