2017-10-12 61 views
2

我假設這必須是一個內存問題,但我不確定。該程序通過PDF循環查找損壞的文件。當一個文件被破壞時,它會將該位置寫入一個txt文件供我稍後查看。當第一次運行它時,我將通過和失敗情況記錄到日誌中。在67381條日誌條目之後,它停止。然後我改變了這個邏輯,所以它只記錄錯誤,但是,在控制檯中,我顯示了一個循環的計數,所以我可以告訴程序有多遠。大約有19萬個文件循環,每次停留在67381處。它看起來像python程序仍在後臺運行,因爲內存和CPU持續波動,但很難確定。我現在也不知道它是否仍將錯誤寫入日誌。Python循環計數停在67381

下面是代碼,

import PyPDF2, os 
from time import gmtime,strftime 

path = raw_input("Enter folder path of PDF files:") 
t = open(r'c:\pdf_check\log.txt','w') 
count = 1 
for dirpath,dnames,fnames in os.walk(path): 
    for file in fnames: 
     print count 
     count = count + 1 
     if file.endswith(".pdf"): 
      file = os.path.join(dirpath, file) 
      try: 
       PyPDF2.PdfFileReader(open(file, "rb")) 
      except PyPDF2.utils.PdfReadError: 
       curdate = strftime("%Y-%m-%d %H:%M:%S", gmtime()) 
       t.write (str(curdate) + " " + "-" + " " + file + " " + "-" + " " + "fail" + "\n") 
      else: 
       pass 
       #curdate = strftime("%Y-%m-%d %H:%M:%S", gmtime()) 
       #t.write(str(curdate) + " " + "-" + " " + file + " " + "-" + " " + "pass" + "\n") 

t.close() 

編輯1:(新代碼) 新的代碼和相同的問題:

import PyPDF2, os 
from time import gmtime,strftime 

path = raw_input("Enter folder path of PDF files:") 
t = open(r'c:\pdf_check\log.txt','w') 
count = 1 
for dirpath,dnames,fnames in os.walk(path): 
    for file in fnames: 
     print count 
     count = count + 1 
     if file.endswith(".pdf"): 
      file = os.path.join(dirpath, file) 
      try: 
       with open(file,'rb') as f: 
        PyPDF2.PdfFileReader(f) 
      except PyPDF2.utils.PdfReadError: 
       curdate = strftime("%Y-%m-%d %H:%M:%S", gmtime()) 
       t.write (str(curdate) + " " + "-" + " " + file + " " + "-" + " " + "fail" + "\n") 
       f.close() 
      else: 
       pass 
       f.close() 
       #curdate = strftime("%Y-%m-%d %H:%M:%S", gmtime()) 
       #t.write(str(curdate) + " " + "-" + " " + file + " " + "-" + " " + "pass" + "\n") 

t.close() 

編輯2:我想現在從運行此不同的機器具有更強大的硬件和不同版本的Windows(10 Pro而不是服務器2008 R2),但我不認爲這是問題。

+1

'PyPDF2.PdfFileReader(open(file,「rb」))'不保證關閉文件。使用上下文管理器使文件的句柄關閉(不會受傷) –

+1

它如何停止?默默? –

+0

是的,它只是凍結,python程序運行在任務管理器仍然和CPU和內存在改變,但等待很長時間後沒有任何反應。 – HMan06

回答

0

嘗試編輯其中一個.pdf文件以使其變大。這樣,如果程序「停止」的循環編號較小,則可以將問題識別爲內存問題。

否則,它可能是一個異常大型PDF文件,它將您的程序一段時間來驗證完整性。

調試這一點,你可以打印你打開找到這個特定.pdf和手動打開進一步調查.pdf文件的文件位置..

0

想通了。這個問題實際上是由於隨機的和非常大的損壞的PDF。所以這不是一個循環問題,它是一個損壞的文件問題。