我假設這必須是一個內存問題,但我不確定。該程序通過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),但我不認爲這是問題。
'PyPDF2.PdfFileReader(open(file,「rb」))'不保證關閉文件。使用上下文管理器使文件的句柄關閉(不會受傷) –
它如何停止?默默? –
是的,它只是凍結,python程序運行在任務管理器仍然和CPU和內存在改變,但等待很長時間後沒有任何反應。 – HMan06