2017-10-30 96 views
3

私は多くのPDFを開いていますが、解析した後にPDFを削除したいのですが、プログラムが実行されるまでファイルは開いたままです。 PyPDF2を使用して開かれたPDfを閉じるにはどうすればよいですか?PdfFileReaderファイルを閉じる方法はありますか?

コード:

def getPDFContent(path): 
    content = "" 
    # Load PDF into pyPDF 
    pdf = PyPDF2.PdfFileReader(file(path, "rb")) 

    #Check for number of pages, prevents out of bounds errors 
    max = 0 
    if pdf.numPages > 3: 
     max = 3 
    else: 
     max = (pdf.numPages - 1) 

    # Iterate pages 
    for i in range(0, max): 
     # Extract text from page and add to content 
     content += pdf.getPage(i).extractText() + "\n" 
    # Collapse whitespace 
    content = " ".join(content.replace(u"\xa0", " ").strip().split()) 
    #pdf.close() 
    return content 

答えて

2

だけオープンし、ファイルを閉じ、自分

f = open(path, "rb") 
pdf = PyPDF2.PdfFileReader(f) 
f.close() 

PyPDF2 .read() sのあなたが右のコンストラクタで、渡すストリーム:with構文はあなたのためにそれを行うことが好ましいです。初期のオブジェクト構築の後、ファイルを投げることができます。

コンテキストマネージャは、あまりにも、動作します:私は自分のコードでこの答えを使用することにしました

with open(path, "rb") as f: 
    pdf = PyPDF2.PdfFileReader(f) 
do_other_stuff_with_pdf(pdf) 
+0

。ありがとう! – SPYBUG96

1

これをやって:

pdf = PyPDF2.PdfFileReader(file(path, "rb")) 

を使用すると、ハンドルへの参照をパージングしているが、あなたは、ファイルが閉じられますときに制御することはできません。

は、私ははい、あなたはPdfFileReaderへのストリームに渡している

with open(path,"rb") as f: 

    pdf = PyPDF2.PdfFileReader(f) 
    #Check for number of pages, prevents out of bounds errors 
    ... do your processing 
    # Collapse whitespace 
    content = " ".join(content.replace(u"\xa0", " ").strip().split()) 
# now the file is closed by exiting the block, you can delete it 
os.remove(path) 
# and return the contents 
return content 
1

書くでしょう、あなたはそれを閉じることができます。

ここから匿名でそれを渡すのではなく、ハンドル付きコンテキストを作成する必要があります。

def getPDFContent(path): 
    with open(path, "rb") as f: 
     content = "" 
     # Load PDF into pyPDF 
     pdf = PyPDF2.PdfFileReader(f) 

     #Check for number of pages, prevents out of bounds errors 
     max = 0 
     if pdf.numPages > 3: 
      max = 3 
     else: 
      max = (pdf.numPages - 1) 

     # Iterate pages 
     for i in range(0, max): 
      # Extract text from page and add to content 
      content += pdf.getPage(i).extractText() + "\n" 
     # Collapse whitespace 
     content = " ".join(content.replace(u"\xa0", " ").strip().split()) 
     return content 
関連する問題